I need to allocate a very large array is there a way to allocate an array without this types of errors? note that I have 8 GB of memory without using vector
this is exactly the error
Unhandled exception at 0x771515ee in __ Microsoft C++ exception:
std::bad_alloc at memory location 0x0049f904..
and this is a chunk of my code
#include<cstdlib>
int main()
{
size_t arraySize =1024*1024*1024;
int *a= new int[arraySize];
for (int i = 0; i < arraySize; i++)
{
a[i] = 3;
}
return 0;
}
/* Thanks guys the solution for my problem is to create a new solution platform with x64 bit operation*/
You are trying to allocate a 4GB block in a 32 bit process. That’s not possible. You are limited to somewhere between 2GB and 4GB addressable space, but in reality much less will be available in a single contiguous block.
If you really need such a large block in a single array then you’ll want to switch to a 64 bit process. Reconsidering your algorithm is likely to be the best solution.