Sizeof() a Vector

sizeof() a vector

You want vector::size() and set::size().

Assuming v is your vector, do this:

size_t size = 0;
for (vector<set<char> >::const_iterator cit = v.begin(); cit != v.end(); ++cit) {
size += cit->size();
}

sizeof() is giving you the in-memory size of the object/type it is applied to, in multiples of sizeof(char) (usually one byte). If you want to know the in-memory size of the container and its elements, you could do this:

sizeof(v) + sizeof(T) * v.capacity(); // where T is the element type

The operation of the sizeof operator in C++

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.

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)

C++: Using sizeof() division to find the size of a vector

The reason why vector.size() isn't constexpr is that std::vector grows as you add data to it; it has a variable size that is not known at compile time. (That's what constexpr means, that the value of the expression is known at compile time.)

What sizeof (vector) gets you is the size of the in-memory representation of the vector class, which has nothing to do with how many elements are stored in it. This obviously can't be correct because vector is a class name, not a variable name, so it can't possibly return the size of any one specific vector.

Using sizeof int_vec, which by the way does not require parentheses, as sizeof is an operator and not a function, is a little more rational, since at least you're asking about the size of some specific vector and not all vectors in general. But again the problem is that you're going to get the size of the in-memory representation, which has to be fixed. After all, you can instantiate one on the stack, and so it has to be of some fixed size so that C++ knows how much space to allocate for it. It can't just expand in place as you add elements to it, because it would overwrite other data on the stack. It has to have some internal pointer to dynamically-allocated memory for storing the elements. When you do sizeof int_vec you're getting the space needed for this pointer and other internal data, not the space needed for the elements themselves.

If you want an array-like container that has a size that's fixed at compile time, try std::array.

sizeof() std::vector (C++)

What do you mean by size of the vector? The size of the vector object is just

sizeof(vec);

If you are interested in how much memory the vector has allocated on the heap, you can use

vec.capacity()*sizeof(T)

So, if you add these, you'll get how much memory you've "lost" because of the vector.

vec.capacity()*sizeof(T) + sizeof(vec)

Please note that exactly how much memory is allocated is implementation-dependent. It's just that the formula above will be practically correct (or approximately correct) on most if not all implementations.

The difference between sizeof and .size() for a vector c++

.size() returns the number of elements in the vector. That's what you should be using.

sizeof gives you the number of bytes used by the object definition, excluding any additional storage it has allocated through the use of pointers. It is a static constant generated by the compiler at compile time, based on the declaration of the vector class.



Related Topics



Leave a reply



Submit