Disclaimer: I might have misunderstood how arrays are created, allocated and initialised. If so, please try to read between the lines.
If I create an array by doing
int array[15] = {0};
All elements of array
the compiler will initialise the elements to 0
. How is this done by the compiler? Does the compiler insert a loop that iterates the allocated memory and sets each element to zero? Does size impact performance? I.e. does it takes longer time to initialise an array of double size (30)? How much longer? Double? Logarithmic? Power of 2?
Only asking out of curiosity.
I assume, you are talking about global variables/arrays.
Actually, it is not compiler task (if we are talking about modern OSes). Compiler just marks this object as zero-initialized.
Then, linker puts this object in section, that should be initialized with zeros. In ELF this section is named .bss
. Usually, this section has no data (because there are only zeroes), but it has size.
And then, when loader loads your program into memory, it knows, that it should zero out memory that belongs to .bss
section.
If we are talking about stack-based (local) variables, then compiler just calls memset()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With