Solved

Memory use and vectors

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 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