list size

why can not get sizeof of a list? Is it like Type *array; that you can not get the size of array and just return the size of pointer which point ro first element of array?

sizeof return 16 for both sizeof(C1/2)=16 and sizeof(char)=1 ,sizeof(char)=8.why sizeof for any list type return 16byte?

inorder to get size of a list i should multiply list.size by sizeof(list type)???

std::list<char*> C1;
std::list<char> C2;


Last edited on
std::list probably contains two pointers, one to the first node and one to the last node. If the size of a pointer is 8 bytes that adds up to 16 bytes. In C++11 it would also have to keep track of the number of elements so if you compile the code in C++11 it would probably give you a size of 24 bytes.

If you want to get the number of elements stored in the list you can use the size member function.
Last edited on
Why 16 byte ?
I don't really know but this is the STL source code

template <class _Tp>
struct _List_node {
  typedef void* _Void_pointer;
  _Void_pointer _M_next;
  _Void_pointer _M_prev;
  _Tp _M_data;

So I think this struct must be 16 byte in some cases
It's a template. You can't just generelized the size. It changes based on your type

Yes, I think it's the only way I can think of
int q = C1.size() * sizeof( char*);

I don't think I ever need to find out about the size of a list...
Well, that's my case
yours must be different

I am sorry if I gave you the wrong answers
Last edited on
Peter87, on 32-bits, the size of a pointer is 4 bytes. Being there 4 pointers (suppose a pointer-sized _M_data variable) the size of the struct is 16 byte, because you are getting the list's size, not the content's size.
Last edited on
The code that rmxhaha posted is for the nodes. The size of a node will be different for different types.
Last edited on
Correct me if I am wrong
From looking at STL List

class list is inhereited from a class named _List_Base
Which only have 1 member

_List_node<_Tp>* _M_node;

It's really is hard reading trough the source code
I don't think I can help you
I am sorry
It is hard to read through the source code of the STL because you're not supposed to do it. The problem here is that the OP is seeing implementation-defined behavior.
> because you're not supposed to do it. (read STL source code)
┬┐why not?

@OP: iirc sizeof is computed at compile time
There's nothing wrong looking at the C++ library headers (or even sources), just keep in mind that every implementation is different. Personally, I like LLVM libc++ code best:
Topic archived. No new replies allowed.