C++ sizeof Vector is 24?
While the public interface of std::vector
is defined by the standard, there can be different implementations: in other words, what's under the hood of std::vector
can change from implementation to implementation.
Even in the same implementation (for example: the STL implementation that comes with a given version of Visual C++), the internals of std::vector
can change from release builds and debug builds.
The 24 size you see can be explained as 3 pointers (each pointer is 8 bytes in size on 64-bit architectures; so you have 3 x 8 = 24 bytes). These pointers can be:
- begin of vector
- end of vector
- end of reserved memory for vector (i.e. vector's capacity)
How to explain the value of sizeof(std::vectorint)?
The compiler cannot optimise away an empty member. It is explicitly forbidden by the standard.
Complete objects and member subobjects of an empty class type shall have nonzero size
An empty base class subobject, on the other hand, may have zero size. This is exactly how GCC/libstdc++ copes with the problem: it makes the vector implementation inherit the allocator.
How do I determine the size of my array in C?
Executive summary:
int a[17];
size_t n = sizeof(a)/sizeof(a[0]);
Full answer:
To determine the size of your array in bytes, you can use the sizeof
operator:
int a[17];
size_t n = sizeof(a);
On my computer, ints are 4 bytes long, so n is 68.
To determine the number of elements in the array, we can divide
the total size of the array by the size of the array element.
You could do this with the type, like this:
int a[17];
size_t n = sizeof(a) / sizeof(int);
and get the proper answer (68 / 4 = 17), but if the type ofa
changed you would have a nasty bug if you forgot to change
the sizeof(int)
as well.
So the preferred divisor is sizeof(a[0])
or the equivalent sizeof(*a)
, the size of the first element of the array.
int a[17];
size_t n = sizeof(a) / sizeof(a[0]);
Another advantage is that you can now easily parameterize
the array name in a macro and get:
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int a[17];
size_t n = NELEMS(a);
C++ How to get sizeof of parent datatype?
The size of the vector itself (as given by
sizeof
) is constant, and will not be affected by how many elements it holds. Differentiate between its "logical size" and "physical size". After all,sizeof(std::vector<int>)
must evaluate to something, and there aren't any object in the vector, there isn't a vector at all. The contents of the vector only affect the value returned by the memberstd::vector::size
.So the size of each structure will be something constant. You just need to obtain it after both structures are fully defined. You can accomplish that with a constructor. Instead of a default initializer.
struct A {
struct B {
std::vector<int> some_Vector;
size_t A_size;
B();
};
B struct_B;
};
A::B::B() : A_size{sizeof(A)}, some_Vector{} {
}
Though, to be frank, I see little reason why anyone would want to capture the sizeof
a structure and hang on to it at run-time. It is, as I already said, constant.
Capacity of Vectors?
The capacity()
of a vector is better expressed as "the amount of space currently set aside that can be used for storage", expressed in terms of the number of potential elements that can be stored. Note that like size()
, this is the number of elements, not the number of bytes.
For example, a vector might have a size()
of 3 and a capacity()
of 4: this says that it is currently storing 3 elements, and has room for a maximum of 4 elements (total) before it will need to reallocate.
On the other hand, sizeof
returns the number of bytes required for an object of the given type, and is determined at compile time. For example, if you want the number of bytes currently allocated by the vector for storage, you could do vector.capacity() * sizeof(vector[0])
.
Related Topics
Is Substitution Performed on a Variadic Parameter Pack Type If the Pack Is Empty
Will Memcpy or Memmove Cause Problems Copying Classes
Why to Use Higher Base for Implementing Bigint
Compiling Code Containing Dynamic Parallelism Fails
Static Array Class Variable "Multiple Definition" C++
"_Gfortran_Pow_C8_I4" Error When Linking .O Files from G++ and Gfortran Using G++
Can the Hwnd from Createwindow/Createdialog Be Getmessage'D from Another Thread
Explicitly Exporting Shared Library Functions in Linux
How to Test Whether a Number Is a Power of 2
How Would One Call Std::Forward on All Arguments in a Variadic Function
Skipping Expected Characters Like Scanf() with Cin
How to Expand Environment Variables in .Ini Files Using Boost
Why Does Rand() Return the Same Value Using Srand(Time(Null)) in This for Loop
Reference Invalidation After Applying Reverse_Iterator on a Custom Made Iterator
C/C++: Casting Away Volatile Considered Harmful