sizeof(std::vector) v/s sizeof(std::array)
我对 sizeof(std::vector
和 sizeof(std::array
感到困惑。
int main(int argc, char const *argv[]) {
std::vector<int64_t> vec_1(2);
std::cout << sizeof(vec_1) << std::endl; // 24
std::vector<int64_t> vec_2(5);
std::cout << sizeof(vec_2) << std::endl; // 24
std::array<int64_t, 2> arr_1;
std::cout << sizeof(arr_1) << std::endl; // 16
std::array<int64_t, 5> arr_2;
std::cout << sizeof(arr_2) << std::endl; // 40
return 0;
}
从上面可以看出,sizeof(std::vectorsizeof(std::array
的情况并非如此。为什么?它们都是STL容器。
即使对于所有其他 STL 容器,例如 std::map
、std::unordered_map
等,我注意到无论元素总数如何,sizeof
都保持不变。那么为什么对于 std::array
来说,它会根据元素的数量而变化呢?如果这就是它的设计方式,那么为什么要这样呢?
I'm confused between the sizeof(std::vector<T>)
and sizeof(std::array<T, N>)
.
int main(int argc, char const *argv[]) {
std::vector<int64_t> vec_1(2);
std::cout << sizeof(vec_1) << std::endl; // 24
std::vector<int64_t> vec_2(5);
std::cout << sizeof(vec_2) << std::endl; // 24
std::array<int64_t, 2> arr_1;
std::cout << sizeof(arr_1) << std::endl; // 16
std::array<int64_t, 5> arr_2;
std::cout << sizeof(arr_2) << std::endl; // 40
return 0;
}
As can be seen above sizeof(std::vector<T>)
remains constant irrespective of a number of elements. However, that's not the case for sizeof(std::array<T, N>)
. Why? Both of them are STL containers.
Even for all the other STL containers like std::map<T1, T2>
, std::unordered_map<T1, T2>
, etc, I noticed that the output of sizeof
remains constant irrespective of a total number of elements. Then why does for std::array<T, N>
, it changes based on the number of elements? If that's how it's designed then why so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为数组的大小取决于其元素的数量,但指针的大小并不取决于它所指向的数组中元素的数量。
std::array
是 C 风格固定数组的包装器。它直接包含其元素。您可以将其视为如下所示:由于
sizeof(T[N])
随N
变化,因此sizeof(std::array
) 也会变化。 T,N>)
。另一方面,std::vector 是动态分配数组的包装器。它只是保存一个指向其数组的指针,在运行时将实际数组分配到内存中的其他位置。这意味着它看起来像这样:
由于
sizeof(T*)
和sizeof(size_t)
都不随count
变化,因此sizeof( std::vector)
是常量。Because the size of an array depends on its number of elements, but the size of a pointer doesn't depend on the number of elements in the array it points to.
std::array
is a wrapper for a C-style fixed array. It directly contains its elements. You can think of it as looking something like this:Since
sizeof(T[N])
varies withN
, so too willsizeof(std::array<T, N>)
.std::vector
, on the other hand, is a wrapper for a dynamically allocated array. It just holds a pointer to its array, allocating the actual array at runtime elsewhere in memory. That means it looks something like this:Since neither
sizeof(T*)
norsizeof(size_t)
vary withcount
,sizeof(std::vector<T>)
is constant.std::array
不允许您添加更多元素。它是一个固定大小的数组,其大小在编译时已知,因此直接存储该数组。这可以防止访问数据时出现不必要的间接访问——数据就在那里。它还具有其他优点,使其更接近传统的 C 数组。std::vector
和其他容器需要间接,因此您看到的大小包括用于该间接的指针。最终,您的向量将达到其容量并需要分配更多内存。在将数据保留在向量对象本身内的同时,您无法做到这一点 - 必须在编译时知道对象大小,以便编译器知道要为其分配多少堆栈空间。您可以设置一个最大大小,并将那么多元素放入容器对象本身,但这是一个不同的容器,目前标准库中不存在。std::array
doesn't let you add more elements. It's a fixed-size array whose size is known at compile-time, so the array is stored directly. This prevents a needless indirection when accessing the data—it's right there. It also has other benefits that bring it closer to a traditional C array.std::vector
and the other containers require an indirection, so the size you see includes a pointer used for that indirection. Eventually, your vector's going to hit its capacity and need to allocate more memory. You can't do that while keeping the data within the vector object itself—the object size must be known at compile-time so that the compiler knows how much stack space to allocate for it. You could have a maximum size and put that many elements into the container object itself, but that's a different container that doesn't exist in the standard library at this time.