Given this piece of code:
(void)someFunction(void) {
int array[] = {1,2,3,4,5,6,7,8,9,10};
}
Where are the values of the array stored?
I'm asking because I'm unsure regarding this question:
Create an array of integers property in objective-c
The array itself will be on the stack. There will be a bit of code on entry to the function that copies the values which are stored in some unnamed array in a global/static memory area into the local array on the stack. It's likely that the unnamed global/static array with the initial values is in the same general memory area as string literals.
Note however, that none of this is required by the standard - but it's pretty much how any compiler will implement it. A smart enough compiler might notice that the values are simply incremented integer values and might gen up a loop to init the local array,
(this answer assumes C/C++ - I don't know if Objective-C would change any of this).
As Micheal has said, where the values of the initializer list will be stored depends on the compiler and the optimization levels. For example, gcc without optimizations will just generate the code to move constants to the appropriate locations in the array, ie the actual values will only be stored in the code itself. You can easily check what will happen if you don't compile to object code, but let your compiler output assembler instead.
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