Reputation: 85
I am trying to develop an application which allocates given number of GB's from the RAM. When i try to allocate 1 GB RAM it works fine but, more than 1GB, it crashes.
I am using Windows 7 - 64bit and I have 6 GB RAM (Installed Memory). I develop this application with Qt and I am using Visual Studio 2013.
Code is here:
int size = 1024 * 1024 * 1024;
m_Size = ui.CPUAllocationEntry->text().toInt();
m_Arr =(char*) malloc(sizeof(char)*size*m_Size);
memset(m_Arr, 1, size*m_Size);
if (m_Arr == NULL)
{
qDebug() << "Pointer Null" << endl;
}
else
qDebug() << "Pointer NOT null" << endl;
return;
And the error is:
First-chance exception at 0x0F993FD4 (msvcr120d.dll) in ttool.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x0F993FD4 (msvcr120d.dll) in ttool.exe: 0xC0000005: Access violation writing location 0x00000000.
I tried this as an 32-bit application but also 64-bit application and the result was the same. Also i tried /LARGEADRESSAWARE option for the linker. However i could not find a solution.
I know it may be about how much RAM i am using but when i using 3.02GB RAM and try to allocate 2GB, it crashes too.
How can i solve this problem?
Upvotes: 2
Views: 1672
Reputation: 10336
Your problem is that you're using the type int
, which is a 32-bit integer, in a multiplication which causes integer overflow (1024 * 1024 * 1024 * 2 produces a negative result with a 32-bit int
).
You should be using size_t
instead (presumably your OS is 64-bit whether or not your app is).
Upvotes: 2
Reputation: 1879
Your memory is fragmented. You are asking for one big block of 1GB, but no such block is available. There may very well be ten 512MB blocks available, but that's not good enough.
Upvotes: 5