全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:952
推到 Plurk!
推到 Facebook!

有關TList的錯誤,請大家幫忙看…

答題得分者是:william
danielj
初階會員


發表:65
回覆:135
積分:40
註冊:2003-06-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-07-25 11:19:34 IP:61.220.xxx.xxx 未訂閱
小弟想將某一組data structure記錄在TList中以供後面程式取用某個data structure內容,但是不知道怎麼寫才對,我初步先把structure內容載入MemoryStream中,然後再將MemoryStream加入TList中(因為我只成功加入過MemoryStream到TList中),程式碼如下: typedef struct { AnsiString Name ; int Size ; } myStruct ; void __fastcall TForm1::Button1Click(TObject *Sender) { TMemoryStream *tmp = new TMemoryStream; myStruct *data = new myStruct; data->Name = "test1"; data->Size = 100; TMemoryStream *stream = new TMemoryStream; stream->Write(data,sizeof(myStruct)); mylist->Add(stream); tmp = (TMemoryStream *)mylist->Items[0]; data1 = (myStruct *)tmp ;//預期data1的內容應該要與data一樣才對,但是沒有一樣 delete tmp; delete stream; delete data1; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { mylist = new TList ; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { delete mylist; } //--------------------------------------------------------------------------- //再加一段測試是否Global也有效 void __fastcall TForm1::Button2Click(TObject *Sender) { myStruct *data2 = new myStruct ;//小弟測試時執行到此會出現Access Violation錯誤 data2 = (myStruct *)mylist->Items[0]; Memo1->Lines->Add("Name" data2->Name); Memo1->Lines->Add("Size" IntToStr(data2->Size)); } //--------------------------------------------------------------------------- 發表人 - danielj 於 2003/07/25 11:33:57 發表人 - danielj 於 2003/07/25 11:38:35 發表人 - danielj 於 2003/07/25 11:58:31
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-07-25 12:00:53 IP:147.8.xxx.xxx 未訂閱
Your structure is not fixed size since AnsiString are dynamic string. What you store to the stream was the pointer of the string only....
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-07-25 12:21:09 IP:147.8.xxx.xxx 未訂閱
This is a LAZY solution using ShortString  < class="code"> typedef struct { ShortString Name ; int Size ; } myStruct ; void __fastcall TForm1::Button1Click(TObject *Sender) { myStruct *data = new myStruct; data->Name = "test1"; data->Size = 100; TMemoryStream *stream = new TMemoryStream; stream->Write(data,sizeof(myStruct)); mylist->Add(stream); delete data; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { mylist = new TList; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { int i; for (i=0;iCount; i) delete ((TMemoryStream *) mylist->Items[i]); delete mylist; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { TMemoryStream *tmp; myStruct *data2 = new myStruct; tmp = (TMemoryStream *)mylist->Items[0]; tmp->Position = 0; tmp->Read(data2,sizeof(myStruct)); Memo1->Lines->Add("Name" data2->Name); Memo1->Lines->Add("Size" IntToStr(data2->Size)); } 發表人 - william 於 2003/07/25 12:22:40
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-07-25 13:19:08 IP:218.160.xxx.xxx 未訂閱
不知是不是你要的.  
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *tmp = new TMemoryStream;
myStruct *data = new myStruct;
myStruct *data1;
TList *mylist = new TList;
data->Name = "test1";
data->Size = 100;
TMemoryStream *stream = new TMemoryStream;
stream->Write(data,sizeof(myStruct));
mylist->Add(stream->Memory);
//tmp = (TMemoryStream *)mylist->Items[0];
//data1 = (myStruct *)tmp ;//預期data1的內容應該要與data一樣才對,但是沒有一樣
data1 = (myStruct *)mylist->Items[0]; // 如此就一樣了
delete mylist;
delete tmp;
delete stream;
delete data1;
}
系統時間:2024-11-23 7:40:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!