如果全局定义了向量,什么时候分配内存空间

发布于 2024-12-29 17:37:26 字数 1831 浏览 0 评论 0原文

如果我全局定义并初始化一个大向量,那么它的大小是否会包含在目标文件中?

例如

情况1:如果我有一个未初始化的大型全局数组x,它的大小正确显示在bss段大小中,但它不会被添加到对象文件大小,因为它是未初始化的数据,这是预期的。

#include <iostream>
#define SIZE 200000000

char x[SIZE];

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762            572        200000040        200002374        bebcb46        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 7477 Jan 28 02:52 a.out

情况2: 类似地,如果我有一个大的初始化全局数组,它的大小将包含在数据段中(不在bss中),并且它也会反映目标文件的大小符合预期。

#include <iostream>
#define SIZE 200000000

//remaining entries will be value initialized
char x[SIZE] = { 'a', 'b' };

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762        200000600             24        200002386        bebcb52        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 200007533 Jan 28 02:34 a.out

case 3: 现在,如果我使用初始化的大型全局向量而不是初始化的全局数组,那么我期望出现类似于 case 2< /em> (obj 文件大小包括初始化数组大小)而是 我得到以下行为

#include <iostream>
#include <vector>
#define SIZE 200000000

std::vector<char> x(SIZE, 'c');

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
4936            604             48           5588           15d4        a.out

$ls -l a.out

 -rwxrwxr-x. 1 ur ur 13583 Jan 28 02:44 a.out

谁能解释一下这种行为,为什么初始化的向量不存在于目标文件中,以及如何在运行时初始化全局定义的向量。我想我在这里遗漏了一些基本的东西。谢谢

If I define and initialize a large vector globally, then will its size be included in object file?

for e.g

case 1: If I have an uninitialized large global array x, its size correctly shows up in bss segment size but it will not be added to object file size as it is uninitialized data , this is expected.

#include <iostream>
#define SIZE 200000000

char x[SIZE];

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762            572        200000040        200002374        bebcb46        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 7477 Jan 28 02:52 a.out

case 2: similarly if I have large initialized global array, its size will be included in data segment(not in bss), and it will also reflect in size of object file as expected.

#include <iostream>
#define SIZE 200000000

//remaining entries will be value initialized
char x[SIZE] = { 'a', 'b' };

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
1762        200000600             24        200002386        bebcb52        a.out

$ls -l a.out

-rwxrwxr-x. 1 ur ur 200007533 Jan 28 02:34 a.out

case 3: Now if instead of initialized global array I use a initialized large global vector, then I am expecting a behaviour like case 2 (obj file size includes initialized array size) but instead
I get following behaviour

#include <iostream>
#include <vector>
#define SIZE 200000000

std::vector<char> x(SIZE, 'c');

int main (void)
{
  return 0;
}

$size a.out

text           data            bss            dec            hex        filename
4936            604             48           5588           15d4        a.out

$ls -l a.out

 -rwxrwxr-x. 1 ur ur 13583 Jan 28 02:44 a.out

Can anyone please explain this behaviour, that why initialized vector is not present in object file, and how a globally defined vector is initialized at run time. I think I am missing something fundamental here. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青衫负雪 2025-01-05 17:37:26

std::vector 在运行时在堆上分配其存储空间。如果您像这样将一个变量定义为全局变量,则其构造函数将在 main() 启动之前运行。根据您提供的参数,构造函数将在堆上为 SIZE 字符分配空间,然后运行循环将字母 c 存储到每个字符中。

目标文件中存在的所有内容都是传递给向量构造函数的字母 c 的单个副本。

std::vector allocates its storage on the heap, at runtime. If you define one as a global variable like that, its constructor will run sometime before main() starts. With the arguments you gave it, the constructor will allocate space for SIZE characters on the heap and then run a loop to store the letter c into each of them.

All that's present in your object file is the single copy of the letter c that's passed to the vector's constructor.

踏月而来 2025-01-05 17:37:26

向量类型通常只包含几个指针(三个是常见的数字:开始/结束/容量)并根据需要动态分配内存。基本上只有 sizeof(std::vector) 会在变量的上下文中,而 capacity()*sizeof(type) 将从堆。

The vector type usually contains just a few pointers (three is a common number: begin/end/capacity) and allocates memory dynamically as needed. Basically only sizeof(std::vector<type>) will be in the context of the variable, while capacity()*sizeof(type) will be dynamically allocated from the heap.

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