简体   繁体   中英

The operation of the sizeof operator in C++

On my MS VS 2015 compiler, the sizeof int is 4 (bytes). But the sizeof vector<int> is 16. As far as I know, a vector is like an empty box when it's not initialized yet, so why is it 16? And why 16 and not another number?

Furthermore, if we have vector<int> v(25); and then initialize it with int numbers, then still the size of v is 16 although it has 25 int numbers! The size of each int is 4 so the sizeof v should then be 25*4 bytes seemingly but in effect, it is still 16! Why?

The size of each int is 4 so the sizeof v should then be 25*4 bytes seemingly but in effect, it is still 16! Why?

You're confusing sizeof(std::vector) and std::vector::size() , the former will return the size of vector itself, not including the size of elements it holds. The latter will return the count of the elements, you can get all their size by std::vector::size() * sizeof(int) .

so why is it 16? And why 16 and not another number?

What is sizeof(std::vector) depends on implmentation, mostly implemented with three pointers . For some cases (such as debug mode) the size might increase for the convenience.

std::vector is typically a structure which contains two elements: pointer (array) of its elements and size of the array (number of elements).

As size is sizeof(void *) and the pointer is also sizeof(void *) , the size of the structure is 2*sizeof(void *) which is 16 .

The number of elements has nothing to do with the size as the elements are allocated on the heap.

EDIT: As MM mentioned, the implementation could be different, like the pointer, start , end , allocatedSize . So in 32-bit environment that should be 3*sizeof(size_t)+sizeof(void *) which might be the case here. Even the original could work with start hardcoded to 0 and allocatedSize computed by masking end so really dependent on implementation. But the point remains the same.

sizeof is evaluated at compile time, so it only counts the size of the variables declared in the class, which probably includes a couple of counters and a pointer. It's what the pointer points to that varies with the size, but the compiler doesn't know about that.

The size can be explained using pointers which can be: 1) begin of vector 2) end of vector and 3) vector's capacity. So it would be more of like implementation dependent and it will change for different implementation.

You seem to be mixing "array" with "vector". If you have a local array, sizeof will provide the size of the array indeed. However, vector is not an array. It is a class, a container from STL guaranteeing that the memory contents are located within a single block of memory (that may get relocated, if vector grows).

Now, if you take a look at the std::vector implementation, you'll notice it contains fields (at least in MSVC 14.0):

 size_type _Count = 0;
 typename _Alloc_types::_Alty _Alval;   // allocator object (from base)
 _Mylast
 _Myfirst

That could sum up to 16 bytes under your implementation (note: experience may vary).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM