sizeof(std::vector) v/s sizeof(std::array)

发布于 2025-01-13 22:40:11 字数 1014 浏览 0 评论 0原文

我对 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::vector)无论元素数量如何,都保持不变。但是,sizeof(std::array) 的情况并非如此。为什么?它们都是STL容器。

即使对于所有其他 STL 容器,例如 std::mapstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2025-01-20 22:40:12

因为数组的大小取决于其元素的数量,但指针的大小并不取决于它所指向的数组中元素的数量。

std::array 是 C 风格固定数组的包装器。它直接包含其元素。您可以将其视为如下所示:

template <typename T, size_t N>
class array
{
    T arr[N];

    // member functions
};

由于 sizeof(T[N])N 变化,因此 sizeof(std::array) 也会变化。 T,N>)


另一方面,std::vector 是动态分配数组的包装器。它只是保存一个指向其数组的指针,在运行时将实际数组分配到内存中的其他位置。这意味着它看起来像这样:

template <typename T, /* other stuff */>
class vector
{
    T* arr;
    size_t count;

    // member functions and such
};

由于 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:

template <typename T, size_t N>
class array
{
    T arr[N];

    // member functions
};

Since sizeof(T[N]) varies with N, so too will sizeof(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:

template <typename T, /* other stuff */>
class vector
{
    T* arr;
    size_t count;

    // member functions and such
};

Since neither sizeof(T*) nor sizeof(size_t) vary with count, sizeof(std::vector<T>) is constant.

凡尘雨 2025-01-20 22:40:12

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文