Sign in to follow this  

vector::reserve at runtime

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

This topic is 4597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this