Arduino Memory Limit

What happens when you go over the Arduino memory limit? I’m trying to debug a problem where a value in my program sometimes goes back to 0. Are there errors in the compilation stage, or does everything just go weird?


Everything just goes weird. You can't really determine exactly how much memory a program uses without running it. But see: this function for getting an estimate of how much you're using.

I've been using this technique to see how much memory (heap) I have left. Since the stack grows down and the heap up, the most common error I've experienced is (naturally) that these collide, which will result in really weird bugs.

uint8_t *ptr = (uint8_t *)malloc(4);
uint8_t *stackptr =  (uint8_t *)(SP);
printf_P(PSTR("heap ptr: %p\n"), ptr);
printf_P(PSTR("stackptr: %p\n"), stackptr);

If you're using the stack also to store return addresses, etc., then what you're describing is very much possible. On my Motorola 68HCS12 this is precisely what happens, and it mostly goes back to 0 only to reset and freeze.

If you're not using it to store return addresses, really they should just be overwriting each other -- which isn't good, but shouldn't cause it to crash. Of course, if you get to the point where you're starting to address negative memory locations, THEN you're going to have a problem (in all honesty, they should be treated unsigned if I remember correctly, so you'll just be accessing memory that's not really there).

EDIT: But I just realized that's not the OP =(

So, OP, what exactly are you doing that could cause you to run out of memory? That's quite a bit of processing to do on a RISC microcontroller.