3

Why the following code returns 12 in codeblocks 13.12 and 20 in visual studio 2010? I am also confused why it returns any other value than 0 as vectors are dynamic and i have not pushed back any elements.

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
using namespace std;

class c
{
public:
    vector<int>v;
    c()
    {
        v.clear();
    }
};
int main() {
    int m;
    c ob;
    cout<< sizeof(ob);

}
Farsan Rashid
  • 1,460
  • 4
  • 17
  • 28
  • 3
    Think about how you would implement a vector, and you may understand why the size is very unlikely to ever be 0. As a minimum it would contain, for example, a pointer and a counter. Remember, sizeof a vector is not the same as sizeof the vector contents. – icabod Oct 21 '15 at 09:57
  • 4
    btw its the compiler that compiles your code, not the IDE – 463035818_is_not_an_ai Oct 21 '15 at 09:58
  • Also this is not because a vector doesn't have any element that it doesn't consume space. I don't know the implementation of vector but it might have its own internal attributes : pointer to the stored data (even if it's just NULL), the number of elements (even if it's just 0),... – nlko Oct 21 '15 at 09:59
  • See: http://stackoverflow.com/questions/17299951/c-vector-what-happens-whenever-it-expands-reallocate-on-stack/17302443#17302443 – Pixelchemist Oct 21 '15 at 10:44

4 Answers4

2

Because sizeof operator shows you the in-memory size (in bytes) of the object representation of given type. Considering that the vector container consists not only of data (there might be counters, pointers, etc) then the exact size in memory depends on implementation in compiler you are using.

And in your case there are different compilers depend on chosen IDE:

  • Visual C++ in Visual Studio
  • MinGW (most likely) in CodeBlocks

If you want to know the exact size (in bytes) of data inside your vector then you can use following method:

int vector_size = sizeof(int) * v.capacity();
maxteneff
  • 1,523
  • 12
  • 28
  • 1
    It is not really the IDE as the original question assumed, but it is also not the "compiler". It is the std library headers that are responsible for the variation in sizeof std::vector. That size might also differ between debug and release builds (because of conditional compilation in those headers). If you use a compiler, such as ICC, without its own headers, or you use any compiler with the headers shipped with a different compiler, most such size changes follow the headers, not the compiler. – JSF Oct 21 '15 at 10:25
1

This is not a matter of IDE but of compiler as comments says.

Whatever, obj is certainly not of size 0, because it contains at least a vector. An empty vector simply does not contains any element, but this does not means that the memory it uses is 0, it may have some hidden necessary attributes.

Different sizes may be due to : different padding applied by different compilers, and different implementations of vectors on different development environments.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

A std::vector can be implemented in different ways, but all of them require three data members. One possibility is equivalent to

template<typename Tp>
class vector
{
  typedef Tp*pointer;
  pointer begin_memory, end_used, end_allocated;
};

which corresponds to sizeof(vector)=3*sizeof(pointer). However, alternatively you can replace either or both of end_used and end_allocated with members of type size_t. This layout and the hardware dependent sizeof(pointer) and sizeof(size_t) determine the sizeof(vector): the size in memory used by a std::vector<>.

On a 32bit system, it's common to have sizeof(pointer)=sizeof(size_t)=4 and hence sizeof(vector)=12.

Walter
  • 44,150
  • 20
  • 113
  • 196
0

I want to add something regardless of the vector. Even int has not a universal size among all compilers(or what you called an IDE). int is at least 4 bytes. So, it may be larger.

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160