Avatar of tantormedia
tantormedia
Flag 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.
C++

Avatar of undefined
Last Comment
tantormedia

8/22/2022 - Mon
jkr

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

ASKER
No, 32.
jkr

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
tantormedia

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:
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
Zoppo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
tantormedia

ASKER
Thanks, this is exactly what happened.