Storage location of C++ array in memory

This question already has an answer here:

  • Where are static variables stored (in C/C++)? 16 answers

  • a static array (declared at a global scope or in a namespace) will be placed in the data segment. A local array declared inside a function scope will be placed on the stack.

    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;
    }
    

    (Same as in plain old C)

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


    是的,以这种方式声明的本地数组将被存储在堆栈中并具有固定长度。


    A local array is addressed in the stack. There is a constant size which can't be increased. If you write more values in the array than it can contain, there will be a so called stack overflow. Behind them fields, there is the memory of other values which would be overwritten then. Visual Studio creates some protection bytes to avoid this.

    链接地址: http://www.djcxy.com/p/87906.html

    上一篇: LInux / c ++,如何同时保护两个数据结构?

    下一篇: C ++数组在内存中的存储位置