Jump to content
  • Advertisement
Sign in to follow this  
startreky498

vector::reserve at runtime

This topic is 4957 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'm trying to put together a storage class using vectors. The problem is that there is no way to know in advance how much to reserve() from the beginning of the program. I need to increment the reserve at runtime. I've tried doing this, but for some reason using vector.reserve(vector.size() + 1) corrupts the data in the vector (the addresses become invalid). The data in the vectors is, in fact, initialized correctly, so that's not the problem. How would I go about reserving a vector at runtime? Do I need to use a different theory concerning keeping track of the vector size?

Share this post


Link to post
Share on other sites
Advertisement
Yeah, as long as you ask it to resize to the current size + the 'extension'. It adds or removes elements.

ace

Share this post


Link to post
Share on other sites
Quote:
Original post by startreky498
I've tried doing this, but for some reason using vector.reserve(vector.size() + 1) corrupts the data in the vector (the addresses become invalid). The data in the vectors is, in fact, initialized correctly, so that's not the problem.


Anything you put into a vector needs to be copyable. That means you either need a copy constructor, or it needs to be a simple type that doesn't require one.

Share this post


Link to post
Share on other sites
Quote:
Original post by startreky498
I'm trying to put together a storage class using vectors. The problem is that there is no way to know in advance how much to reserve() from the beginning of the program. I need to increment the reserve at runtime.


Also, be aware that you may not need to do any reservation at all. reserve() is really only provided to allow you to optimize for speed by avoiding reallocation, when you know how much space you need. If you *don't* know...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!