局部变量的值消失了
value of local variable is gone?
我有这个实现:
//header file:
InfoTables* localInforTable;
typedef txdr_int32 InfoTable;
typedef struct
{
int sendID;
InfoTable *data;
} InfoTables;
// in cpp file
void Retrieval::InfoTableCallBack(int sendID,
InfoTables& infoTables)
{
localInforTable = new InfoTables();
localInforTable.sendId=sendID;
localInforTable->data = infoTables.data;
printf("Data %d, %dn", localInforTable.sendId, localInforTable->data[0]); // correct data
}
void Retrieval::CheckInfoData()
{
printf("Data %d, %dn", localInforTable.sendId, localInforTable->data[0]); // sendID is OK but data9[0] is just printing the address
}
我想将方法InforTableCallBack中的inforTables复制到一个本地变量中,以便用于其他方法。但是,数据是在CheckInfoData()中清理的?
代码中存在各种错误。首先,data
不指向任何已分配的内存。第二,memcpy
根本不适用于用户定义的类型,这些类型通常是不可复制的。您可以使用MyData
的赋值运算符:
void myMethod1(Mydata &otherdata)
{
*data = otherdata;
}
添加到juancapanza的答案:
假设代码有拼写错误(如果真的是memcpy()
)
因为CCD_ 6已经是指针,所以CCD_。和'&'指针上是指针的地址=memcpy
的简单错误使用
相关文章:
- 使用的未初始化局部变量'Quick'
- 修复未初始化的局部变量错误
- 局部变量保留函数中的值
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 将引用分配给局部变量,如果局部变量超出范围,它会超出范围吗?
- Gnuplot_i.hpp C++接口绘制局部变量而不是文件
- 如何在函数外部访问函数中局部变量的值?
- 赋予全局变量而不是局部变量优先级的函数 - (异常行为)
- C++中静态方法的局部变量范围
- 未初始化的局部变量错误甚至认为我初始化了它(C++)
- 离开范围后如何保护局部变量的值?
- 局部变量名称冲突
- 我创建的类似乎错误地设置了它的局部变量
- 使用了未初始化的局部变量"lc",但我对其进行了初始化
- 是否释放了局部变量的地址
- 在计算函数返回值之前或之后是否销毁了局部变量
- 编译器是否优化了内联函数的局部变量
- C++返回了对析构函数的未定义引用和对局部变量的引用
- 调用fscanf后发生了奇怪的局部变量变化
- 局部变量的值消失了