How to find out the size of std::list?

my os : windows xp sp3()32bits
compilers : gcc4.5(minGW)
1
2
3
  std::list<size_t> haha;
  haha.resize(10);
  std::iota(haha.begin(), haha.end(), 0); //initialize haha to 0~9 


I want to find out how many memory allocated for each node
2 pointers plus one size_t, it should be 4 + 4 + 4 = 12bytes
But I expect it would be more than 12bytes
How could I find out the size of the list occupy by the default allocator?
Thanks a lot
In general you can't know that unless you want to write your own allocator that counts. Even if you knew how much space the allocator allocates, you should understand that the C++ runtime may allocate more anyway. For example:

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    int* p = new int;
    int* q = new int;
    std::cout << p << ' ' << q << std::endl;
}


outputs 0x9867008 0x9867018 for me. Subtracting the two pointer values yields 0x10, or 16 decimal, despite the fact that sizeof(int) == 4. This is because the minimum heap block size is 16 bytes. Every new will consume at least 16 bytes of memory, even if you only allocate 1 byte.
Thanks a lot, maybe someday I have to implement my allocator
But looks like boost already do that for us?
Last edited on
Topic archived. No new replies allowed.