Page 1 of 1

Allocating Memory in CMyApplication

PostPosted: Jan 16, 2004 @ 5:41am
by mlepage
If I take the minimal application (GapiDraw 2.05eval), and add one line to CMyApplication to allocate an array:

int array[240 * 240];

then when I launch the application, it locks the device before the splash screen is displayed.

If I drop it down to this amount:

char array[240 * 240];

then I get an actual dialogue which says illegal exception, and the device does not hang.

If I drop it down to this amount:

char array[240 * 210];

Then it works fine.

If I increase it to this amount:

char array[240 * 225];

Then I get the spinning wait cursor, for half a minute before I lose patience and reset the device.

So I assume I'm running out of memory. Yet 240 * 240 * 1 byte is only 56KB, which doesn't seem like a lot. Wouldn't surfaces take up 240 * 320 * 2 bytes which is even more?

Does anyone else see this behaviour? Can they allocate a 240x240 array of chars? ints? I'm using a release build for iPAQ 3970.

PostPosted: Jan 16, 2004 @ 11:48am
by ppcStudios

PostPosted: Jan 16, 2004 @ 2:24pm
by mlepage

PostPosted: Jan 16, 2004 @ 2:35pm
by Pam

PostPosted: Jan 16, 2004 @ 2:47pm
by mlepage

PostPosted: Feb 17, 2004 @ 3:52pm
by Layre5150
I believe you are taxing the stack. If for example you have:

char cMyArray[128];
char cMyArray[128*100];

Which is allocated from the stack and is totally different than:

char* pMyArray = (char*)malloc( 128 * 100 * sizeof(char) );

Which comes from the heap.

PostPosted: Feb 17, 2004 @ 3:55pm
by Layre5150
I took a look at the date on that post which was posted about a month ago - sorry. I guess you probably would have solved it by now.

PostPosted: Feb 18, 2004 @ 4:22am
by mlepage
Yup it was on the stack and I didn't really notice it was. :-)

In my older project I also allocated in CMyApplication, but I actually created the application instance on the heap so it was fine.

In my newer project, however, the instance is created on the stack just like the Minimal sample applicaiton.

PostPosted: Feb 18, 2004 @ 1:47pm
by Layre5150

PostPosted: Feb 18, 2004 @ 5:11pm
by mlepage
Well, I only allocate this at the beginning of the program run. So it's not a problem allocating and deallocating.

Further, if you allocate statically, apparently that would be a problem if you port to devices like Symbian.

The correct way to approach memory problems is to only write your own allocators once you've shown allocation to actually be a problem.

Right now it's not a problem for me, my app runs quite well.