Link to home
Start Free TrialLog in
Avatar of tantormedia
tantormediaFlag for United States of America

asked on

Memory allocation problem

Dear experts,

In my MFC application, I am trying to allocate memory like this:

short* pLeftoversFromPrevIteration = 0;
...
pLeftoversFromPrevIteration = 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.
Avatar of jkr
jkr
Flag of Germany image

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?
Avatar of tantormedia

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.
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:
pLeftoversFromPrevIteration = 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;      
pLeftoversFromPrevIteration = 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.
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, this is exactly what happened.