在64位系统中分配超过4GB的内存 [英] Allocating more than 4GB memory in a 64 bit system
问题描述
我运行这个代码,编译64位vc ++ 2005,在Windows Server 2008 R2与32GB。在for循环中有一个访问冲突。
#include< iostream>
using namespace std;
int main(int argc,char * argv [])
{
double * x = new double [536870912];
cout<< memory allocated<< endl
for(long int i = 0; i <536870912; i ++)
{
cout< i<< endl
x [i] = 0;
}
delete [] x;
return 0;
}
所以如果在新的double [536870912]中没有异常,
另一点值得一提的是,这个程序在另一台计算机上成功测试。
$ b- long int是32位:这意味着你的最大值是2147483647和sizeof(双)* 536870912> = 2147483647.(我真的不知道这是否有意义,它可能取决于编译器的工作原理)
- 您的分配失败。
我建议您测试以下代码:
#include< conio.h>
#include< iostream>
using namespace std;
#define MYTYPE unsigned long long
int main(int argc,char * argv [])
{
//测试编译模式
if(sizeof(void *)== 8)cout<< 编译64位< endl
else cout<< 编译32位< endl
//测试mytype的大小
cout<< Sizeof:< sizeof(MYTYPE)< endl
MYTYPE len;
//获取<<< doubles>>分配
cout<< 你想要多少双? << endl
cin>> len;
double * x = new(std :: nothrow)double [len];
//测试分配
if(NULL == x)
{
cout< 无法分配< endl
return 0;
}
cout<< memory allocated<< endl
//将所有值设置为0
for(MYTYPE i = 0; i {
if(i%100000 == 0 )cout < i<< endl
x [i] = 0;
}
//在发布之前等待,测试内存使用
cout<< 按< Enter>键以继续...;
getch();
//可用内存。
delete [] x;
}
编辑:使用这个代码, 9GB的区块。
I'm running this code, compiled on 64 bits vc++ 2005, on Windows Server 2008 R2 with 32GB. There is an access violation inside the for loop.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
double *x = new double[536870912];
cout << "memory allocated" << endl;
for(long int i = 0; i < 536870912; i++)
{
cout << i << endl;
x[i] = 0;
}
delete [] x;
return 0;
}
So if there is no exception in new double[536870912], why am I getting an access violation when doing an assignment over a particular array position?
Another point worth mentioning is that this program was succesfully tested on another computer.
It is probably one of the following problem:
- long int is 32-bits: that mean your maximum value is 2147483647, and sizeof(double)*536870912 >= 2147483647. (I don't really know if that has sense. It probably depend on how the compiller work)
- Your allocation is failing.
I suggest you to test the following code:
#include<conio.h>
#include <iostream>
using namespace std;
#define MYTYPE unsigned long long
int main(int argc, char* argv[])
{
// Test compiling mode
if (sizeof(void*) == 8) cout << "Compiling 64-bits" << endl;
else cout << "Compiling 32-bits" << endl;
// Test the size of mytype
cout << "Sizeof:" << sizeof(MYTYPE) << endl;
MYTYPE len;
// Get the number of <<doubles>> to allocate
cout << "How many doubles do you want?" << endl;
cin >> len;
double *x = new (std::nothrow) double[len];
// Test allocation
if (NULL==x)
{
cout << "unable to allocate" << endl;
return 0;
}
cout << "memory allocated" << endl;
// Set all values to 0
for(MYTYPE i = 0; i < len; i++)
{
if (i%100000==0) cout << i << endl;
x[i] = 0;
}
// Wait before release, to test memory usage
cout << "Press <Enter> key to continue...";
getch();
// Free memory.
delete [] x;
}
Editing: Using this code, i just achieved allocate a single block of 9GB.
这篇关于在64位系统中分配超过4GB的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!