This question already has answers here:
Where are static variables stored in C and C++?
                                
                                    (16个回答)
                                
                        
                                4年前关闭。
            
                    
我知道在C ++中调用new等同于在C中调用malloc,并且在调用free时可以释放malloc函数返回的指针。通过调用'new'int [length]获得的数组存储在堆中。但是,如果一个静态数组,例如int x [3] = {1,2,3},该怎么办?被宣布?这样的数组将存储在哪里?堆?

最佳答案

静态数组(在全局范围或命名空间中声明)将放置在数据段中。
在函数作用域内声明的本地数组将放置在堆栈上。

int g_global_array[2] = {4,5,6};  //Data Segment
int main() {
     int local_array[3] = {1,2,3};  //Stack
     static int s_static = 10; //Also in the Data Segment (static)
     return 0;
}


(与普通旧C相同)

http://www.geeksforgeeks.org/memory-layout-of-c-program/

10-06 04:49
查看更多