Solved

STL Vector overhead

Posted on 1998-04-20
7
1,085 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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.

809 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