?
Solved

Memory use and vectors

Posted on 2006-06-22
2
Medium Priority
?
194 Views
Last Modified: 2010-08-05
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
Comment
Question by:JohnSantaFe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16962899
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 16962913
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question