简体   繁体   中英

when will memory space be allocated if vector is defined globally

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

for eg

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM