I was running a sketch where I needed to check the amount of available memory after certain operations/function calls. To my surprise, I got strange results.
The below (trimmed down) sketch reported more memory than the Nano actually has. The freeMemory() function was taken from Measuring Memory Usage | Memories of an Arduino | Adafruit Learning System
Sketch
void setup()
{
Serial.begin(115200);
Serial.println(F("String (capital S) memory test"));
Serial.print(F("Free RAM = "));
Serial.println(freeMemory());
}
void loop()
{
}
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory()
{
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
Output
String (capital S) memory test
Free RAM = 2299
Anybody any idea?