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.
long int
is 32 bits on 64-bit Windows, so the loop will never terminate. You should change the type ofi
tosize_t
to be sure it's big enough for any array index. I've no idea whether that's the only problem, though. – Smorgasbord4GB
consecutive block in there. – Sosthenna2^32
though. – Dewyeyednew[]
cannot allocate the requested amount then it should throw an exception at the allocation point. – Mcavoy536870912 * sizeof(double)
yields 0 for me. Looks like a wrap around insideoperator new
. – Hospitaldouble *x = new (std::nothrow) double [536870912]
. – Mcavoy&x[i]
). Wouldn't surprise me if it was on a page boundary. – Mcalpine