tantormedia
asked on
Memory allocation problem
Dear experts,
In my MFC application, I am trying to allocate memory like this:
short* pLeftoversFromPrevIteratio n = 0;
...
pLeftoversFromPrevIteratio n = new short[chunkSize];
I am getting a "Debug Error": Invalid allocation size: 4294967295 bytes.
But in the debugger I can see that chunkSize is 4294889547 bytes which is valid.
Though if I go deeper via Call Stack, I can see:
> Book.exe!operator new[](unsigned int nSize=4294967295, const char * lpszFileName=0x00a43d54, int nLine=1618) Line 67 + 0x13 bytes C++
Could you please explain this to me?
Thanks.
In my MFC application, I am trying to allocate memory like this:
short* pLeftoversFromPrevIteratio
...
pLeftoversFromPrevIteratio
I am getting a "Debug Error": Invalid allocation size: 4294967295 bytes.
But in the debugger I can see that chunkSize is 4294889547 bytes which is valid.
Though if I go deeper via Call Stack, I can see:
> Book.exe!operator new[](unsigned int nSize=4294967295, const char * lpszFileName=0x00a43d54, int nLine=1618) Line 67 + 0x13 bytes C++
Could you please explain this to me?
Thanks.
Um, you are trying to allocate 4294889547 bytes or 4095MB or 3.99GB? That cannot work, since your whole process' address space is 4GB on Windows (32bit) with only 2GB available to your application. Are you using Win64?
ASKER
No, 32.
Well, then that is bound to fail. The theorhetical maximum you can allocate is 2GB or 2147483648 bytes, yet you'd have to substract the amount of memory taken up by your application itself.
ASKER
But still, why does debugger sees that number as greater than it really is? (In fact, that greater number is maximum value of size_t).
Also,
My original code was this:
pLeftoversFromPrevIteratio n = new short[chunkSize];
chunkSize -= iTotal;
That allocation did not fail, at least no error was reported. Though when I tried to memcpy to that array, memcpy crashed.
Then I noticed that the code should actually look like this:
chunkSize -= iTotal;
pLeftoversFromPrevIteratio n = new short[chunkSize];
And because of some reason, now I am getting that allocation problem. Could you please give me any explanation of why the allocation looked to be working first?
Thanks.
Also,
My original code was this:
pLeftoversFromPrevIteratio
chunkSize -= iTotal;
That allocation did not fail, at least no error was reported. Though when I tried to memcpy to that array, memcpy crashed.
Then I noticed that the code should actually look like this:
chunkSize -= iTotal;
pLeftoversFromPrevIteratio
And because of some reason, now I am getting that allocation problem. Could you please give me any explanation of why the allocation looked to be working first?
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, this is exactly what happened.