Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

STL Vector overhead

Posted on 1998-04-20
7
Medium Priority
?
1,095 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

610 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