Thread: how to get the size of an STL container object?!!!

  1. #1
    Registered User sam-j89's Avatar
    Join Date
    May 2009
    Posts
    14

    how to get the size of an STL container object?!!!

    Hi guyz..
    I just wanna ask about the size allocated for the objects instantiated from a class container in the STL, for example when I write :
    Code:
    cout<<sizeof(list<int>);
    I get the result 24, or when i write:
    Code:
     cout<<sizeof(vector<int>);
    I get the result 20, whatever the type of the vector or the list (or any other containers) is set : int , char, double …. I will always get the same.. why!!!
    So if I wanna actually know the size of my STL container object .. how would I get it?!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use the .size() method.

    sizeof() just tells you how many bytes the "head" data structure contains.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The containers allocate memory internally in a dynamic fashion with pointers and calls to new/delete. When you use sizeof on a container, you do not get the size of the memory that has been allocated via these processes, just the memory of any pointers, and miscellaneous data members the container directly "owns". To get the number of bytes the container is managing it would be whatever the size() function returns (number of objects being stored) multiplied by the size of the object stored within the container (more accurately it would likely be capacity() multiplied by the sizeof the stored objects). That plus the sizeof of the container itself would get the total memory footprint occupied by a particular instance of a container (in two [at least] separate memory locations).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also keep in mind that the total amount of memory used by the container could well exceed that amount, due to other ancillary data (such as inter-nodal pointers). The bottom line is that it varies with implementations.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User sam-j89's Avatar
    Join Date
    May 2009
    Posts
    14
    hi hk_mp5kpdw , thanks for your attention..
    i tried what you told me exactly(capacity() multiplied by the sizeof the stored objects) but unfortunately that didn't work
    cuz when i wanna get the size of an object from the class vector... i still have the same result , which is 20(per element), whatever the set type is. that is whether the type set is : char , integer ,....etc they are the same :20 one element, 40 two elements , 60 three elements.... and so on!!!!
    Last edited by Salem; 05-12-2009 at 09:40 AM. Reason: Removed unnecessary bold

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by sam-j89
    cuz when i wanna get the size of an object from the class vector...
    Size of an object in the vector? Or the total size of the vector plus the contents?

    Quote Originally Posted by sam-j89
    i tried what you told me exactly(capacity() multiplied by the sizeof the stored objects) but unfortunately that didn't work
    Plus the sizeof the container itself. And that's only a general guideline. Containers of containers or objects which themselves manage memory make things difficult.

    Code:
    std::vector<int> intVect;
    std::cout << "Empty    : " << sizeof(intVect) + sizeof(int) * intVect.capacity() << std::endl;
    
    intVect.push_back(20);
    std::cout << "1 Element: " << sizeof(intVect) + sizeof(int) * intVect.capacity() << std::endl;
    
    intVect.push_back(30);
    std::cout << "2 Element: " << sizeof(intVect) + sizeof(int) * intVect.capacity() << std::endl;
    Code:
    Empty    : 20
    1 Element: 24
    2 Element: 28
    Which equates to 20 bytes for the vector<int> itself plus 4 bytes for each int stored within.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you ACTUALLY trying to do? In other words, why do you think it makes sense to know the size of the contents of a vector or list container?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User sam-j89's Avatar
    Join Date
    May 2009
    Posts
    14
    mmmm i see.. now i got it
    thank you so much bro hk_mp5kpdw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. inherited classes and stl container
    By rahulsk1947 in forum C++ Programming
    Replies: 9
    Last Post: 05-05-2007, 02:27 PM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  4. STL size thread safe
    By vasanth in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 11:44 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM