PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Allocating Memory in CMyApplication

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.
Hi Marc. If you want to zip up the project and email it to me I'll run it through boundschecker and see what it says on the PC side. I allocate a ton more memory than that for Warlords, but that is under the 2.04 release. Have you tried that release (its available on my website if you don't have it - look in the gapidraw forum).
Thanks G.R. I started evaluating 2.05 just over a week ago before 3.0 was announced.

I allocate way more memory in 2.04 with another program. So I wonder if the problem is only with 2.05, only with the eval version, or particular to the way I'm allocating it here (normally I use STL containers).

As I said, it works on the PC, but not the PPC.

I'll try it under 2.04 today. As for trying it yourself, you need only add that single allocation line of code to the CMyApplication class in a copy of the minimal sample. It will then try to allocate when the application object is created in WinMain.
Are you adding the array as a Class member?

In my experience, an array of that size should be allocated dynamically in InitInstance or CreateSurfaces.

It has something to do with allocating on the Stack vs. the Heap, and the limited Stack size, a subject I don't fully understand I admit.

Pam
OK I just tried it in 2.04 with the same problem.

Then I tried STL containers: vector of vector of char, 240 by 240, and it was fine. I figured that since the memory requests weren't as large that way, it would work if memory was available but not contiguous.

So I also tried a single vector of 240^2 chars, which would be contiguously allocated, and it also was fine.

So finally I realized the problem is simply allocating on the stack as opposed to on the heap. As Pam just pointed out. :-) (My understanding is that the stack is limited in size. It grows whenever you call a function, and that's also where auto storage variables are stored (and their members). It's also the reason you don't want to recurse too deeply.)

My quick prototype code doesn't use STL containers everywhere, that's the problem. A few arrays are small but this one was larger. I'm going to have to go back and convert everything to STL containers as they should be. Luckily the project is very small, and I mostly access data through interfaces, and even then vector notation is compatible with arrays, this won't be much of a hassle.
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.
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.
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.
Cool. With the memory on a PDA device being very precious, you might also want to look into writing some memory management stuff. There are various types ranging from very complex to very simple.

For example, a frame based approach were you, at the start of the app, allocate one single (but huge) chunk of memory, then later dishing it out as needed. This does however abandon the idea of a "free()" or "delete" call in the dust (until the application is done you just make a single call to "free()"). This way, after allocating all that memory, if the application starts then you know you will never have any "out of memory" errors.

In the end, you would only have 1 call to malloc and 1 call to free (from the system that is). Things can be a little tricky when you need temporary buffers but in the end you will cut out potentially dangerous errors such as memory leaks or running out of memory.

Even something so simple as wisely using static memory allocation can improve performance.

Anyways, I've babbled enough - good luck with your projects!
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.