I'm trying to understand how memory is used on the Arduino.
If you look at the web-page http://arduino.cc/en/Tutorial/Memory it states that a declaration like:
char message[] = "I support the Cape Wind project.";
will use SRAM. My program seems to suggest otherwise. The way I am checking free memory came from Arduino Playground - HomePage using:
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
If I allocate arrays, strings, etc, I do not see this reflected in the memory usage.
ie: If I increase/decrease these strings/arrays/etc, there is no change in the free memory.
If I do a malloc(), I do see this change, as expected.
So, my question is, is the Arduino memory web-page incorrect, is the code I'm using above for freeRam() incorrect, or
am I doing something really stupid? Below is my code:
int freeRam() ;
extern void* malloc() ;
void setup() {
char message[] = "I support the Cape Wind project.";
Serial.begin( 9600 ) ;
Serial.print( "debug: Free ram = " ) ;
Serial.println( freeRam() ) ;
Serial.flush() ;
char *str = (char *)malloc( 500 ) ;
Serial.print( "debug: Free ram after malloc = " ) ;
Serial.println( freeRam() ) ;
Serial.flush() ;
}
void loop() {
delay( 1000 ) ;
}
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
If I increase message[], there is no SRAM memory usage change shown.
Possibly related to this, is that if I use Serial.print() to print out strings, I do see the memory being used, however, it is double what I expect. For eg: if I increase a string by 10 bytes, and use Serial.print() to print it out, memory availability will decrease by 20 bytes, even though sizeof() of the string (and of type char) confirms that only 1 byte is being used per character. I assume that this is just because of how Serial.print() is implemented and the lesson here is to be aware that it can use double the memory you may expect.
cheers,
-rj