Solved

Memory use and vectors

Posted on 2006-06-22
2
190 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

785 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