C++ STL List implementation

H

harsh.murari

I wanted to know how the C++ STL list is implemented. Specifically, I
wanted to understand the memory usage by the list objects.

I have the following code snippet:

std::list <int> L;

L.push_back(10);
L.push_back(20);

printf("List size %d\n", sizeof(L));

This is returning the object size of L to be 4 bytes. How is this
possible? What data members does the list object contain? Where are
previous pointer, next pointer etc stored? Any insights would be
appreciated.
 
S

sebastian

This is returning the object size of L to be 4 bytes. How is this possible?

char * buffer = new char[ 1024 ];
cout << sizeof( buffer ) << endl;

obviously, sizeof( ) isn't very useful when applied to pointers. ;)

who knows? the standard doesn't dictate the actual implementation
details.

SGI's implementation is fairly straightforward and quite readable:
http://www.sgi.com/tech/stl/. or simply browse the code that came with
your compiler.
 
J

Juha Nieminen

I wanted to know how the C++ STL list is implemented. Specifically, I
wanted to understand the memory usage by the list objects.

I have the following code snippet:

std::list <int> L;

L.push_back(10);
L.push_back(20);

printf("List size %d\n", sizeof(L));

This is returning the object size of L to be 4 bytes. How is this
possible?

In your compiler std::list is most probably a class which has one
pointer as member variable (a pointer to the actual data).
What data members does the list object contain? Where are
previous pointer, next pointer etc stored?

A list object has no "prev" and "next" pointers. List *nodes* have
them, not the list itself. List nodes are allocated separately and do
not affect the size of the list object in any way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top