Archived

This topic is now archived and is closed to further replies.

nick_coleman

vectors inside structs

Recommended Posts

I''m writing a program that started off as C and swiched to C++, I have a struct that has vector members I malloc the struct, but sometimes I''m getting a seg fault when I try to push_back(item) on the vector. I can change the other variables in the struct and chage. The problem happens when I am dealing withe a pointer to the stuct (called line): line->members.push_back(item); and not necessaritly the first push. Shouldn''t I be able to push on an arbitary number of items? I appreciate any help I was up to 11 in the morning last night and It''s 6:40am here again, and this was due two days ago! Thanks Nick

Share this post


Link to post
Share on other sites
Never use malloc() in C++. The vector constructor isn't being called. use new instead.

Oh, and of course, never use free() either. Use delete or delete[] instead.

[edited by - sneftel on July 16, 2002 3:12:57 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
Never use malloc() in C++. The vector constructor isn''t being called. use new instead.

Oh, and of course, never use free() either. Use delete or delete[] instead.



Well, if you''re implementng operator new() and/or operator delete() [admittedly pretty advanced stuff...], then you''ll
need to fall back to malloc/free to avoid infinite recursion.



~~~~
Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz

Well, if you''re implementng operator new() and/or operator delete() [admittedly pretty advanced stuff...], then you''ll
need to fall back to malloc/free to avoid infinite recursion.



Very true... tho given that he didn''t even know to use new, I''m guessing it''ll be a little while before he has need for overloading new or delete.

Share this post


Link to post
Share on other sites