Memory allocated

Hi all!

I have defined a list of vectors and I am worried because my program is going to add a lot of vectors (the vector contains 7 integer elements). I predict there will be around 50,000. One of the differences between lists and vectors I have seen so far is that you don't have to define a specific size for the list, but... should I worry about the space the compiler gives to the list as a standard? How much space does the compiler gives to a list? or is it variable and depends on the requirements during the execution?

Thank you in advanced, I am quite new with lists
In a std::list<T> each node looks like (approximately):

1
2
3
4
5
struct _list_node {
    _list_node* next;
    _list_node* prev;
    T                  data;
};


sizeof( _list_node ) where T = int is 3 words (12 bytes on a 32-bit platform). However, the minimum allocation unit in the memory management system is usually at least 16 bytes. Meaning that for every 4-byte int you store, you consume an additional 12 bytes for other stuff.

So 16 bytes * 7 nodes per list * 50000 lists = 5,600,000 bytes. There are a few extra bytes of overhead per std::list<> instantiation, but that will be relatively insignificant compared to the 5.6M bytes consumed by the nodes.

---

Since you know the maximum (fixed?) size of the container, and since the elements are just ints, there is hardly any reason not to use std::vector<> over std::list<>. vector's slowest operations -- insert, push_front, and occasionally push_back (when a reallocation is necessary) will perform effectively no worse than any insert into the std::list<>, but push_back can perform better.


Topic archived. No new replies allowed.