1

假设我使用 Visual Studio 编写了一个 C++ 程序,它使用 new 运算符来分配内存。我想知道是否有一种方法可以让 new 自动使用大页面而不是标准的 4KB 页面(即,我的程序没有显式调用 VirtualAlloc)。

谢谢你的时间。

4

2 回答 2

3

您可以覆盖所有新的删除的运算符。例如

void * operator new(size_t size)
{
    return malloc(size);
}

void operator delete(void * pointer)
{
    free(pointer);
}

您还应该覆盖此运算符的所有变体:

于 2013-04-23T08:13:15.147 回答
1

Implementation specific, once again. All libraries are not restricted to doing that since all the standard says AFAIK is that new allocates memory for C++. For Microsoft's implementation, new always calls HeapAlloc.

http://cboard.cprogramming.com/cplusplus-programming/98364-new-invokes-virtualalloc.html

My understanding is that unless you're running in a virtual machine, the OS has full control over the default heap and stack memory allocation. The above link also brings up a good point in line with Raymond's response to your question: Are you sure you need to use large pages? You open yourself to a good deal of internal fragmentation by doing so.

于 2013-04-22T21:41:39.770 回答