Jump to content
  • Advertisement

Archived

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

Hedos

Vector crash when adding an element over the reserve limit

This topic is 5321 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

Hey.. I''m using a vector for my program.. When I first tested my program, I could add one element to the vector with no problem, but when adding a second element , my program would crash... Then I decided to set a reserve() value of 380 to my vector... Everything worked fine untill I got to my 381th element, it crashed with an assert error... I''m using push_back(myElement); to add an element to my vector I have been using vectors before, it always worked correctly.. I don''t know what could cause the problem. Someone has an idea about what could be a cause of the problem? Thanks

Share this post


Link to post
Share on other sites
Advertisement
My guess is that the elements of the vector are moved when adding an element causes reallocation, and your iterators or pointers are no longer valid.

It would be easier to figure out the problem if you included useful important information like the error message from the assert.




[edited by - JohnBolton on February 29, 2004 3:25:07 AM]

Share this post


Link to post
Share on other sites
I agree, you probably have an iterator that you''re not keeping up to date with your vector. Once the vector gets too large, it usually gets a new chunk of memory twice the size of the previous chunk, copies everything over, and frees the old chunk. Since your iterators will be pointing to the old chunk of memory, it doesn''t work.

Share this post


Link to post
Share on other sites
Thanks for your replies. I checked my code and it seems that there is no pointer to any element in my vector.. so this can''t be the problem


Here is the assert error:


Debug assertion failed!

Program: MyProg.exe
File: dbgheap.c
Line: 1017

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Share this post


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

Debug assertion failed!

Program: MyProg.exe
File: dbgheap.c
Line: 1017

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)



That''s a heap corruption error. At some point in your program, you are writing unallocated memory and overwriting memory management structures. Start by looking at the last object that is dynamically allocated before your vector - that is, hopefully, the one that lies right before the vector in memory and, once again, if you''re lucky, the one you are accessing incorrectly.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!