-1

On what basis is size of class object shown as 12?

class testvector
{
    public : vector<int> test;
};
int main()
{
    testvector otestvector;
    cout<<"size :"<<sizeof(otestvector)<<"\n";
    cout<<"size of int :"<<sizeof(int);
}

Output:

size :12
size of int :4
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Suchin Raj
  • 17
  • 2
  • 8
  • The size of an empty `vector` is 12. – Sumner Evans Nov 02 '16 at 04:54
  • if i create vector the size is printed as 20. on what basis is this size assigned. – Suchin Raj Nov 02 '16 at 04:56
  • 4
    It's up to whoever writes the standard library implementation you are using – M.M Nov 02 '16 at 04:56
  • On what basis do you believe there is a basis to assign a size to a class? Why can't it be merely an unimportant consequence of some other things? –  Nov 02 '16 at 05:03
  • yes its based on the standard library implementation, but there must be some logic based on the size of the type of vector that the bytes are allocated. For int the size is printed as 12, why is the size printed as 20 for vector. ( size of bool is one) – Suchin Raj Nov 02 '16 at 05:05
  • Actually, why not? –  Nov 02 '16 at 05:10
  • I can't merely accept that size of vector is 12, when size of int is 4 and size of vector is 20 , when size of bool is 1. there must be some logic based on which these sizes for vector is assigned. M looking for that logic. – Suchin Raj Nov 02 '16 at 05:15
  • 3
    There is no "logic". `sizeof(vector)` and `sizeof(T)` are practically unrelated. –  Nov 02 '16 at 05:29
  • Yes sizeof (vector) and sizeof (T) are unrelated, and the size of vector is independent of the type of T, but then i expect sizeof (vector) and sizeof (vector) to be same, but they are different. what is the reason? – Suchin Raj Nov 02 '16 at 06:29
  • 1
    @SuchinRaj, How is the comment to **selbie**s answer not providing an answer to that? – StoryTeller - Unslander Monica Nov 02 '16 at 06:33
  • May be going through the template specialization concept will give me a clear picture of it. – Suchin Raj Nov 02 '16 at 06:53

1 Answers1

8

Think of it like this. Let's imagine the standard C++ library didn't have a vector class. And you decided it would be a good idea to have one.

You just might, at a very minimum, come up with something like this. (Disclaimer: the actual vector class for C++ is far more complicated)

template <class T>
class vector
{
    T* items;        // array of items
    size_t length;   // number of items inserted
    size_t capacity; // how many items we've allocated


   public:
     void push_back(const T& item) {
         if (length >= capacity) {
             grow(length * 2); // double capacity
         } 
        items[length] = item;
        length++;
     }

    ...
};

Let's break down an instance of my simple vector class down on a 32-bit system:

 sizeof(items) == 4   // pointers are 4 bytes on 32-bit systems
 sizeof(length) == 4; // since size_t is typically a long, it's 32-bits as well
 sizeof(capacity) == 4; // same as above

So there's 12 bytes of member variables just to start out. Hence sizeof(vector<T>) == 12 for my simple example. And it doesn't matter what type T actually is. The sizeof() operator just accounts for the member variables, not any heap allocations associated with each.

The above is just a crude example. The actual vector class has a more complex structure, support for custom allocators, and other optimizations for efficient iterations, insertion, and removal. Hence, likely more member variables inside the class.

So at the very least, my minimum example is already 12 bytes long. Probably will be 24 bytes on a 64-bit compiler since sizeof(pointer) and sizeof(size_t) typically double on 64-bit.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • ok so i understand that type of T will not matter. But then the actual vector class must be a generic class independent of the type of T. so from my understanding i believe the members of the vector class will be same independent of the type of T. So in that context i expect size of vector and size of vector to be same. But they are different. what can be the reason for this? – Suchin Raj Nov 02 '16 at 06:16
  • @SuchinRaj, [`vector`](http://en.cppreference.com/w/cpp/container/vector_bool) is a [template specialization](http://en.cppreference.com/w/cpp/language/template_specialization). It isn't an instance of the primary template, and its implementation can be entirely different. – StoryTeller - Unslander Monica Nov 02 '16 at 06:24
  • 1
    `vector` is sometimes implemented as a bitmask (one bit per item) to be really efficient with memory. So the container might be a few bytes to have members for keeping track of bit packing state. – selbie Nov 02 '16 at 06:48
  • May be going through the template specialization concept will give me a clear picture of it. – Suchin Raj Nov 02 '16 at 06:52
  • 1
    @selbie: It is not *sometimes* implemented as such, but *always*, at least assuming a standard-conforming implementation. – celtschk Nov 02 '16 at 06:54