Solved

STL Vector overhead

Posted on 1998-04-20
7
1,081 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 50 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now