Solved

Memory use and vectors

Posted on 2006-06-22
2
188 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
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now