?
Solved

STL Vector overhead

Posted on 1998-04-20
7
Medium Priority
?
1,104 Views
Last Modified: 2013-12-14
Can anybody tell me what the overhead of using a STL vector template is. For example if I create a vector using a char*
 
std::vector<char*> status_list;
 
and then I set the list to be 10 items long and initialise each pointer to point to a 256 character string how much memory would actually be used up?
 
e.g.
 
for (int x=0;x<10;x++)
{
  char* empty = new char [256];
  strcpy(empty,"Not Used");
  status_list.push_back(empty);

 
something like this maybe: (256*10)+(sizeof(char*)*10) + overhead?
 
What I am eventually trying to achieve is a list which has x spaces all allocated at the start of the program that I can then copy strings into when "things" happen in my program. Like a debug list that can tell me if anything was out of sequence. When I reach the end of the list I will start back at the beginning, but I need to decide how much memory I am prepared to give over to the list before it will overwrite itself.
 
Would it be better just to use an array:
 
char status_list[MAX_ITEMS][256]?

0
Comment
Question by:sdj
7 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1162209
This is very implementation dependent. In the MS STL, I think that each vector, in addition to the space used to store its elements, has 3 pointers (beginning of storage, end of storage, last element).

Also, for each T, the use vector<T> causes an allocation of space by allocator<T>. You can prevent/change this by using your own allocator.

As for using push_back(), most STL implementation will double the size of the vector storage whenever you call push_back AND there is no more available storage. You can avoid this by calling reserve(N).

In any case, whatever the result you come up with, it will be non-portable and may change  when you use a different/newer STL implementation.
0
 
LVL 3

Expert Comment

by:altena
ID: 1162210
ping pong... Correct answer by yonat... Give him an "A"
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162211
Sdj. For the exampe you proposed at the end (debug data list?) I would not use a vector.  Vector's are used when you need an array that can be resized.  or elements can be added, deleted, or moved around.  It seems to me that you indicated that you wanedt to allicate an array of a fixed number of elements and not change the number.  That sounds like a job for a plain old array.  (Although a vector would have onslightly more memory overhead over the array in this case.  Especially since you don't resize it.)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:GregL
ID: 1162212
One additional point.  In most implementations the vector uses an array internally, the three pointers are just locations within that array.  I saw this in the Stroustrup book.
0
 

Author Comment

by:sdj
ID: 1162213
I'd love to award some points here but until somebody answers the question... (that is answers rather than adds a comment, because I can't grade comments.)

Incidently I now have to have a list whose size is decided at runtime, rather than knowing the size at compile time. That wasn't initially a requirement but know it is I guess I will have to use a list of sometype. I believe vector is still the best option.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162214
If you need to decide the list size at runtime, but not resize it after that, then a dynamically allocated array might be better than an vector.  (I say might because a vactor has some convenient functions that you might want to have and accept the extra overhead (negligable) for the conveneince)  If you need to resize it, a vector is probably best.  

I would think that yonat deserves the points.  
0
 
LVL 5

Accepted Solution

by:
yonat earned 200 total points
ID: 1162215
Thanks for being gracious, guys. I guess this is one of those "I which I could split the points among several experts" cases.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

621 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