3

Maybe it's a stupid question. But I want understand it and cannot find an answer. When I write smth like below:

int test[1000000] = {0};

Will this array include in compiled program code? Or only instruction to hold available memory for this array?

I want understand whether C++ includes all array's values in binary code in this case or allocates memory at runtime?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 4
    If this is a global variable - it has static storage duration and will likely be allocated space in the binary code. If it's automatic storage duration (e.g. inside a function) it will be allocated on stack every time the code enters the function. – Rostislav Mar 18 '16 at 17:14
  • Thank you! But why then I allocate 1 element my app has size 9152 bytes but then 10000000 - 9208 byte? How can 9999999 integers numbers stored in 56 bytes? But if 1000000000 then 13xxx bytes... What do I not understand? I have global array. – Denis Sologub Mar 18 '16 at 17:18
  • Or is C++ align data in binary app? – Denis Sologub Mar 18 '16 at 17:21
  • @ghostman: The executable needs not contain more than information about the size of the array and its init data. When the program is loaded in order to run it, the array is created in memory, using up storage. – Cheers and hth. - Alf Mar 18 '16 at 17:22
  • Did you read my comment? – Denis Sologub Mar 18 '16 at 17:23
  • It looks like array's value store in binary code. But app size increases in steps, not linearly. – Denis Sologub Mar 18 '16 at 17:25
  • 2
    you should read this http://stackoverflow.com/questions/1169858/global-memory-management-in-c-in-stack-or-heap – Reousa Asteron Mar 18 '16 at 17:28
  • Sorry, I can bad read English. As I understood from a topic. Executable file uses segments. So, can it has some rule to align a segment in binary code and it means that app size will increase in steps? Is it true? – Denis Sologub Mar 18 '16 at 17:34
  • Thanks all for answers! – Denis Sologub Mar 18 '16 at 17:49

1 Answers1

5

The answer to this question is strongly dependent on the data format in use.

For example, when you write this

int test[1000000] = {1, 2, 3};

and use a compiler that produces an ELF executable, the compiler emits the data for 1, 2, 3, but sets the size of the section to sizeof(test). When ELF executable is loaded into memory, the loader stores 1, 2, 3 in the first four ints, and zeros out the remaining section to the end. See this Q&A for more details on ELF's handling of trailing zeros in a data section.

Other executable formats have similar capabilities: essentially, instead of storing zeros in the text section, they store instructions for the loader to set some static memory aside, and clear it out before executing the program.

Note: The answer assumes that test is allocated in the global scope.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for answer! But why then I use a very big array size then app size becomes much bigger? Compiler cannot allocate enough memory at runtime and allocates it in binary code? – Denis Sologub Mar 18 '16 at 17:40
  • 2
    @ghostman It sounds from your description that the executable format that you are using has a limit on the size of a section. Once you cross that limit (say, by declaring `1000000000` integers) the compiler is forced to produce a file with a different layout - say, use another section, switch to larger section sizes, etc. It does not look like your executable reaches the size of that array, though, because 13 kilobytes is still mush less than the size of yoru array, which is four gigabytes on systems with 32-bit integers. – Sergey Kalinichenko Mar 18 '16 at 17:47
  • I understood. Thank you for detail answer. You very helped me. – Denis Sologub Mar 18 '16 at 17:49