【问题标题】:(C++11) What's the difference between static array and dynamic array with list initialized?(C++11) 初始化列表的静态数组和动态数组有什么区别?
【发布时间】:2014-10-08 22:17:03
【问题描述】:

例如,有一个包含数千个元素的 int 数组:

static int st_indices[9999] = {
   0,  27,  26,    1,  41,  71,    0,  26, 101,    0, 101, 131,    0, 131,  72,
   1,  71, 176,    2,  56, 206,    3, 116, 236,    4, 146, 266,    5, 161, 296,
......
};

int* dy_indices = new int[9999] {
   0,  27,  26,    1,  41,  71,    0,  26, 101,    0, 101, 131,    0, 131,  72,
   1,  71, 176,    2,  56, 206,    3, 116, 236,    4, 146, 266,    5, 161, 296,
......
};

以上两种方式有什么区别,尤其是花括号中的值对内存使用的影响?

我知道 st_indices 将一直存在于内存中,直到程序结束 (STACK),而 dy_indices 将在 delete [](HEAP) 之后释放。还是关于堆栈与 .DATA 段的问题?

【问题讨论】:

    标签: arrays c++11 memory-management memory-segmentation


    【解决方案1】:

    静态在编译时完成..(设置内存量,也称为堆栈)

    动态在运行时完成(动态分配,可以是任何大小,具体取决于系统限制,也称为 HEAP)

    【讨论】:

      【解决方案2】:

      @Dr.Kameleon's answer得知操作系统会读取可执行文件的内容,并将其加载到内存中。

      即花括号中的数据将被加载到内存的 .TEXT 段中。如果我们不考虑虚拟内存/分页,将数据放在文件中然后读入,将减少内存使用(对于 OpenGL 应用程序)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-14
        • 2017-08-27
        • 2013-04-11
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 2019-09-27
        • 2011-01-21
        相关资源
        最近更新 更多