我有這樣實現:局部變量的值不見了?
//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, %d\n", localInforTable.sendId, localInforTable->data[0]); // correct data
}
void Retrieval::CheckInfoData()
{
printf("Data %d, %d\n", localInforTable.sendId, localInforTable->data[0]); // sendID is OK but data9[0] is just printing the address
}
我想在方法InforTableCallBack複製inforTables到一個局部變量,我可以用其他方法。但是,數據在CheckInfoData()中清理?
請發佈*真實*代碼。構建一個[最小測試用例](http://sscce.org)。 –
拋開所有的錯誤,你不是基本上已經在用'memcpy'來做'你想要的'數據嗎?一般來說,您應該使用帶有C++的拷貝構造函數,而不是'memcpy'。 – Dukeling
我已經更新了真實代碼 – olidev