Naiem
Naiem

Reputation: 65

Why std::stack memory size is bigger than as usual in c++?

This is the code for testing my question.

#include <iostream>
#include <stack>
using namespace std;
int main(){
    int num;
    int Array[1];
    stack<int> Stack;

    cout << "Int size " << sizeof(num) <<endl; // Output: Int size 4
    cout << "Array size " << sizeof(num) <<endl; // Output: Array size 4
    cout << "Stack size " << sizeof(Stack) <<endl; // Output: Stack size 80
    return 0;
}

I'm trying to understand about memory space allocation. Normally int memory size is 4 bytes. But, when I initialize an Stack of int data-type in std::stack then the Stack size is 80 bytes.

Should it 4? Why is std::stack taking 80 bytes? Or what is actually inside of stack for being the size 80 bytes?

Upvotes: 2

Views: 191

Answers (2)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29965

sizeof gets the static size of the object/type. stack dynamically allocates memory for its elements. So, there is no correlation between size of the elements and size of stack in general. So, why is it 80 bytes? This is highly implementation specific. Size of stack is usually the same as the underlying container. By default, the underlying container is a std::deque, so that's where we must have a look. I checked libstdc++ specifically, and it seems to have 1 pointer, 1 size_t for size and 2 iterators like so:

struct _Deque_impl_data
{
    _Map_pointer _M_map;
    size_t _M_map_size;
    iterator _M_start;
    iterator _M_finish;
    //...

(std::deque derives from _Deque_base which has a single member of type _Deque_impl_data)

Pointer and integer are 8 bytes, the iterators are 32 bytes. This adds up to 80 bytes. I didn't further investigate, but since deque is a more complex structure, it's only natural that it needs some memory for its own book-keeping.

Upvotes: 5

Adrian Mole
Adrian Mole

Reputation: 51825

You maybe confusing sizeof(Stack) with Stack.size() here. The sizeof operator returns the total size of the class object, which, in the case of std::stack includes (of necessity) a number of internal data and control variables (padding the size out to, in your case, 80 bytes). However, a call to Stack.size() will return the number of items currently on the stack.

These 'internal variables' will include such things as a pointer to the allocated memory (likely 8 bytes), a value recording the current element count (also likely to be 8 bytes) and a number of other pointers and counters, to aid in manipulation of the stack and optimization of access to the contained data, such as the current capacity of the allocated space, etc.

The following modified code shows the difference:

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    int num;
    int Array[1];
    stack<int> Stack;

    cout << "Int size " << sizeof(num) << endl;     // Int size 4
    cout << "Array size " << sizeof(Array) << endl; // Array size 4 (1 "int" element)
    cout << "Stack size " << sizeof(Stack) << endl; // Size of a "std::stack<int>" instance
    cout << "Stack size " << Stack.size() << endl;  // Size (# entries) of stack = 0 (empty)
    return 0;
}

Upvotes: 0

Related Questions