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?
数组的值存储在哪里?
- Stack?
- Heap?
- Together with those string literals. (Is it called High Frequency Heap?)
- Somewhere else?
连同那些字符串文字。 (它被称为高频堆吗?)
I'm asking because I'm unsure regarding this question:
我问,因为我对这个问题不确定:
Create an array of integers property in objective-c
在objective-c中创建一个整数属性数组
3 个解决方案
#1
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).
(这个答案假设C / C ++ - 我不知道Objective-C是否会改变这一点)。
#2
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.
正如Micheal所说,初始化列表的值将存储在哪里取决于编译器和优化级别。例如,没有优化的gcc将生成代码以将常量移动到数组中的适当位置,即实际值将仅存储在代码本身中。如果不编译到目标代码,可以很容易地检查会发生什么,但是让编译器输出汇编程序。
#3
If anyone reading this is looking at it for C#, the answer is different. Arrays are reference types, so the values are stored on the heap and referred to on the stack.
如果有人在阅读这篇文章时正在考虑使用C#,那么答案就不同了。数组是引用类型,因此值存储在堆上并在堆栈上引用。
#1
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).
(这个答案假设C / C ++ - 我不知道Objective-C是否会改变这一点)。
#2
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.
正如Micheal所说,初始化列表的值将存储在哪里取决于编译器和优化级别。例如,没有优化的gcc将生成代码以将常量移动到数组中的适当位置,即实际值将仅存储在代码本身中。如果不编译到目标代码,可以很容易地检查会发生什么,但是让编译器输出汇编程序。
#3
If anyone reading this is looking at it for C#, the answer is different. Arrays are reference types, so the values are stored on the heap and referred to on the stack.
如果有人在阅读这篇文章时正在考虑使用C#,那么答案就不同了。数组是引用类型,因此值存储在堆上并在堆栈上引用。