Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Memory use and vectors

I'm about to use some really large vectors.  Three of them may contain over 1 million doubles.  Two of them are only used for a short period of time.  I want to be memory wise, but I'm not sure how allocation is done with vectors.

The one vector that sticks around I declare in the normal manner and I assume is using the stack.

The other two I declare as pointers and new - delete the memory myself off the heap.

I'm guessing that as I push more elements into the vector it is handling heap allocation even if a standard stack declaration was used.  In which case it doesn't really matter if I new - delete the vector to begin with.

So my question are, does it matter with vectors, or does it all come off the heap anyway?  Are there performance or any other issues to consider? in general when using a structure that gets extremely large, is it better to keep your stack small and use the heap?

Thanks.
0
JohnSantaFe
Asked:
JohnSantaFe
  • 2
1 Solution
 
jkrCommented:
>>I want to be memory wise, but I'm not sure how allocation is done with vectors. [...]
>>So my question are, does it matter with vectors, or does it all come off the heap anyway?

The default allocator will allocate on the heap, so, unless you provide your own implementation (which - if it is not really bonkers - will allocate from the heap also), so you can be sure of that.

>>in general when using a structure that gets extremely large, is it better to keep your stack small and use the
>>heap?

This is always a good idea. E.g. VC++'s default stack size is 2MB, which has just room for 250000 doubles. Large blocks also don't cause the heap to fragment.
0
 
jkrCommented:
BTW, if you know the exact or high water mark amount of data beforehand, it'd be a good idea to call 'reserve()' on the vector to increase performance.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now