只是想知道C++应用程序使用的最大内存是否有限制
我知道这是2GB - 这是正确的吗?
如果一个C++应用程序尝试请求超过2GB内存,这会导致内存崩溃吗?
最后的问题 - 如果运行C++应用程序的机器已经内存不足而且C++应用程序要求100MB的阵列(即连续的内存),操作系统是否会通过虚拟内存来适应这种情况?
每次从stdin读取字母'u'时,此代码段将分配2Gb,并在读取'a'时初始化所有已分配的字符.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#define bytes 2147483648
using namespace std;
int main()
{
char input [1];
vector<char *> activate;
while(input[0] != 'q')
{
gets (input);
if(input[0] == 'u')
{
char *m = (char*)malloc(bytes);
if(m == NULL) cout << "cant allocate mem" << endl;
else cout << "ok" << endl;
activate.push_back(m);
}
else if(input[0] == 'a')
{
for(int x = 0; x < activate.size(); x++)
{
char *m;
m = activate[x];
for(unsigned x = 0; x …Run Code Online (Sandbox Code Playgroud)