Trouble related to array/global memory usage?

Before I get started, let me briefly state that Arduino is a very, very impressive project. The resources are outstanding, and the hardware is very 'accessible'.

Unfortunately I've been noticing some strange behavior from the Arduino (or compiler?) that seems to be directly related to the size of the global variables I setup. If I define an array in my sketch (which is not small):

unsigned long testArray[48];

The following code elsewhere in the sketch (in another tab/file, actually) acts up:

Serial.println(HIGH, DEC); // Result: "2"

Or even:

Serial.print(HIGH, DEC); 
Serial.print(LOW, DEC); 
Serial.println(LOW, DEC); // Result: "210"

If I size down the array to only 16, the code prints out the expected values. However, if I then add another array after it (and never access or modify it), I again get the broken results:

unsigned long noProblem[16];
unsigned long causesAProblem[32];

I have also tried changing the type of the first array to byte (byte testArray[48]; ) and this works fine. Unfortunately I need it to be unsigned long to hold millis() values. I made some effort at cooking up a simple sketch that reproduces the problem, but no luck yet, and there's way too much of my code to post here.

Finally, if I make the second array even larger (128), things go haywire immediately after boot-up (garbage on the serial output).

It seems clear that this is causing something to overrun it's buffer, but I don't know what. I find it particularly odd that even the unreferenced array excites the behavior. Am I just using up too much memory? I have a few other arrays, but my sketch size is only 7130 bytes ("14336 max").

Environment: Arduino NG rev.C USB, Mac OS X 10.4.9.



Remember that program memory (flash) and data memory (RAM) are completely separate on the AVR - the size of your sketch is irrelevant to this problem. It certainly sounds like you're overrunning the 1K of RAM available to you on the ATmega8. That array of 48 long ints uses ~20% of your RAM.


Ahhhhh... well, that makes perfect sense. :) Thanks, kg4wsv.