Jump to content
  • Advertisement

Archived

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

nick_coleman

vectors inside structs

This topic is 5819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I posted this on the beginners board but I''m after an answer in a hurry so I hope no one minds the double post 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
Advertisement
If you''re using C++, you should NOT be using malloc/free. Use new and delete.

new does more than allocate memory; it allocates memory then it calls constructors. Because you malloc''d your struct, the vector''s constructor would not get called and so not initialized properly.

Likewise, delete does more than release memory; it calls the destructor, then frees the memory.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Share this post


Link to post
Share on other sites
Also, if you get a problem on the push_back line, it could be because your pointer "line" is invalid, which wouldn''t have anything to do with the vector operation. Make sure line is still valid--it''s pointing to an actual object and that object hasn''t been deleted.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!