我们可以通过x64中的malloc()获得多少内存?

时间:2013-06-26 04:39:26

标签: c++ c malloc

在x64中,我可以通过malloc()获得超过1.9G的内存,但我的物理内存是8G,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:8)

这很可能是因为您使用的是32位编译器,32位操作系统或(可能)两者。

我简化了你的代码,直到这个:

#include <iostream>
#include <stdlib.h>

int main() {
    void *block = malloc(1024LL * 1024LL * 1024LL * 6);
    if (block)
        std::cout << "Allocated 6 Gig block\n";
    else
        std::cout << "Unable to allocate 6 Gig block.\n";
    return 0;
}

如果我使用32位编译器编译它,它会失败(打印出“无法分配6 Gig块”。如果我使用64位编译器编译它,它会成功(打印出“Allocated 6 Gig block”我没有32位操作系统来测试它,但我有理由相信,对于32位操作系统,它也会失败(32位可执行文件的行为与它在一个64位操作系统,64位可执行文件根本不能在32位操作系统上运行。

Specs: 32-bit compilers tested: gcc 4.8.1 (MinGW), Microsoft VC++ 17.
64-bit compiler: VC++ 17.
OS: Windows 8 x64.
相关问题