0

我的程序可能有内存问题,所以我尝试查找各种​​工具提供的有关内存使用情况的信息。为了找出原因,我也做了一些简单的实验。在发布模式下,我添加以下代码,

pChar = new char[((1<<30)/2)];
for(int i; i < ((1<<30)/2); i++)
{
    pChar[i] = i % 256;
}

执行代码时,Windows 任务管理器中的可用物理内存不会改变。在我看来,编译器可能会删除代码以提高性能。我将变量声明为一个全局变量。它不起作用。但在调试模式下,Windows 任务管理器中的可用物理内存会按预期发生变化。我无法理解。

我有另一个问题。如果物理内存用完,新操作是否会从虚拟内存中分配内存。还是会抛出一个异常?

4

1 回答 1

0

It's indeed quite possible that the compiler detects a "write-only" variable. Since it's non-volatile, the writes can be safely eliminated, and then there's no need for the OS to actually allocate RAM.

new just allocates address space, on modern systems. Physical RAM is allocated when needed. Typically this happens when the ctor runs, as it initializes the members. But in new char there's of course no ctor.

于 2012-04-16T08:05:26.043 回答