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

use*_*809 1 c c++ malloc

在x64上,我可以不度日的malloc()比1.9G内存的更多,但我的物理内存为8G,为什么会出现这种情况?

Jer*_*fin 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;
}
Run Code Online (Sandbox Code Playgroud)

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

Specs: 32-bit compilers tested: gcc 4.8.1 (MinGW), Microsoft VC++ 17.
64-bit compiler: VC++ 17.
OS: Windows 8 x64.
Run Code Online (Sandbox Code Playgroud)