Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lists

Started by Danias Dec 9, 2000 at 1:27 PM 0 replies 550+ views
Original Post
Danias
Danias
Hi folks! I''ve been messing around with lists. Fist of all, I work with BCB5. I use the class TList and use the method ''Add(void *)'' to - surprise! surprise! - add object to the list. Now, the first element in the list is always an ''int *''. The next element is always a pointer to an object of a self-written class. This is how I add the elements to the list: int iKind = 1; int *i = &iKind lstControl1->Add(i); lstControl1->Add(lpGUI_Dolly); When I want to read the elements in the list, the first element is always wrong: ip = (int *) lstTemp->Items; i++; int bType = *ip; although int * ranges from 0-9, the value always exceeds them. Though bType should have the value 1, is has something around 12,000!! If anybody can tell me what I have made wrong, please tell me! Thanks
SiCrane
SiCrane
I think the problem is that you''re passing a pointer to a stack allocated integer. When the block where the integer was declared goes out of scope, the pointer is no longer valid. In order to pass the integer pointer to the TList, you need to dynamically allocate the integer.

i.e.
int * iKind = new int;
*iKind = 1;
lstControl1->Add(iKind);


Just don''t forget to delete the integer when you''re done.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.