搞不明白了 Tlist 和 结构的问题

wheat 2001-09-18 10:04:23
static TList *MyList = new TList; //全局静态

typedef struct AList
{
int I;
char C;
} TAList;

typedef TAList* PAList;

前提:
按两次以上的按钮,取不出数据
void __fastcall TForm1::Button1Click(TObject *Sender)

{
PAList AStruct;
AStruct = new TAList;

if (MyList->Count !=0 ) //判断MyList是不是有数值
{
for (int i = 0; i < MyList->Count; i++)
{
AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; /////重要点,要在这里取出第一ADD到MyList的数组
}

}else{//第一次添加到Mylist中

AStruct->I = 100;
AStruct->C = 'Z';
MyList->Add(AStruct);

AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; //测试、在这里就可以取出来


}
// 删除 AStruct 后,问题在第二次出现,即数值取不出来
delete AStruct;
}
//----------------------------------------------------------------------
// FromClose 时
{
delete MyList;
}

问题是按第二次按钮时,数据按结构的方式,在Tlist中 提取不出来

非常着急
写了这么多,大侠请帮忙或给各思路也行,非常非常感谢

...全文
211 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wingsun 2001-09-18
  • 打赏
  • 举报
回复
你搞错了,问题多多,你的代码我给你改一改。
你在第二次取不到数据是因为在第一次你将其delete了,所以你不能在这儿就delete。
void __fastcall TForm1::Button1Click(TObject *Sender)

{
PAList AStruct;

if (MyList->Count !=0 ) //判断MyList是不是有数值
{
for (int i = 0; i < MyList->Count; i++)
{
AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; /////重要点,要在这里取出第一ADD到MyList的数组
}

}
else
{//第一次添加到Mylist中

AStruct = new TAList;
AStruct->I = 100;
AStruct->C = 'Z';
MyList->Add(AStruct);

AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; //测试、在这里就可以取出来


}
}
//----------------------------------------------------------------------
// FromClose 时
{
PAList AStruct;
for (int i = 0; i < MyList->Count; i++)
{
AStruct = (PAList) MyList->Items[i];
delete AStruct;
}
MyList->Clear();
delete MyList;
}

wheat 2001-09-18
  • 打赏
  • 举报
回复
呵呵,
to LuoGD(沃适) 
这个我已经知道了,我知道错了,呵呵,问题是怎样改进呢
to: whitelion(好好学习,天天向上) 

看了上面的程序,和我的问题关联不是太大。

我美好的实现方法是 每次 new Astruct 可以把他删掉 以为要调用N次这个数据,不然不是都在内存中,还不是一次从数据库里拿出来,我的想法是 既然TList 是可以保存这么多数值,为什么不能再把他取出来呢!!,当然是用结构的数据方式了

LuoGD 2001-09-18
  • 打赏
  • 举报
回复
up
whitelion 2001-09-18
  • 打赏
  • 举报
回复
PAList AStruct;
for (int i=0;i<worklist->Count;i++)
{
//删除原有链表中数据
AStruct=new TAList;
AStruct = (PAList) worklist->Items[i];
delete AStruct;
}
worklist->Clear();
String sdate;
TDate ccdate;
opensql(mysql);
projdata->myquery->First();
int i=0;
while (!projdata->myquery->Eof)
{
//在链表中增加新的数据
ccdate=projdata->myquery->FieldByName("wkday")->AsDateTime;
sdate=DateToStr(ccdate);
AStruct = new TAList;
AStruct->I = i;
AStruct->date =sdate;
worklist->Add(AStruct);
i++;
projdata->myquery->Next();
}
projdata->myquery->Close();
LuoGD 2001-09-18
  • 打赏
  • 举报
回复
delete AStruct;
这句删掉了你保存的数据,你当然取不出来
openworld 2001-09-18
  • 打赏
  • 举报
回复
up
wheat 2001-09-18
  • 打赏
  • 举报
回复
太感谢 HEROIN(HEROIN)了
对于上面你提到的,确实是我程序没有写好,谢谢

可是怎样改进呢,在第二次数据的时,也能提出来,因为上面写的是一个函数,
要调用N次,如果每次new的话,不就爆了
HEROIN 2001-09-18
  • 打赏
  • 举报
回复
删除你new 的AStruct,应该在删除TList的Item[i]时进行

AStruct = (PAList) MyList->Items[i];
delete AStruct

delete MyList->Items[i];



而且,你的程序内存未释放:

PAList AStruct;
AStruct = new TAList;

如果TList有数据,AStruct 就无法释放





HEROIN 2001-09-18
  • 打赏
  • 举报
回复
delete AStruct;
这句删掉了你保存的数据,你当然取不出来
wheat 2001-09-18
  • 打赏
  • 举报
回复
只能给这么多分数,不够在加,谢谢你了


开会去了,半个小时回来,

在这里先谢谢各位MMGG
wheat 2001-09-18
  • 打赏
  • 举报
回复
总结一下:

是的:如上两位上说,当我取数值的时候,是不能再new了。正如 fastcall(小马) 所说Tlist
中保存的是指针,如果我再new的话,就是一个新的地址了。

非常感谢各位,我知道给分是一种方式,也抵挡不住对你们的感激,

thanks all
wheat 2001-09-18
  • 打赏
  • 举报
回复
亲人啊,终于好了,谢谢各位。
fastcall 2001-09-18
  • 打赏
  • 举报
回复
这样应该可以
/*Tlist保存的是指针而不是数据所以不能删除这个指针
/*取出时不需要给分配内存如果 AStruct = new TAList;
/* 后 AStruct = (PAList) MyList->Items[i];
/*则刚分配的内存将丢失

void __fastcall TForm1::Button1Click(TObject *Sender)

{
PAList AStruct;


if (MyList->Count !=0 ) //判断MyList是不是有数值
{
for (int i = 0; i < MyList->Count; i++)
{
AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; /////重要点,要在这里取出第一ADD到MyList的数组
}

}else{
//第一次添加到Mylist中
AStruct = new TAList;
AStruct->I = 100;
AStruct->C = 'Z';
MyList->Add(AStruct);

AStruct = (PAList) MyList->Items[i];
char temp= AStruct->IP; //测试、在这里就可以取出来


}
//----------------------------------------------------------------------
// FromClose 时
{ PAList AStruct;
for (int i=0;i<MyList->Count;i++)
{


AStruct = (PAList) MyList->Items[i];
delete AStruct;
}

delete MyList;
}

13,873

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧