2

Upon compiling a sketch using the Arduino IDE, a message like the following gets displayed in the console:

Global variables use 1540 bytes (75%) of dynamic memory, leaving 508 bytes for local variables. Maximum is 2048 bytes.

In addition, it seems that if the global variables use exceeds around 75% of total dynamic memory, the following message gets displayed in addition:

Low memory available, stability problems may occur.

So, it seems that by using the IDE in a regular way, the compiler does not ensure that local variables memory allocation won't cause any RAM overflow during program execution. Instead, it just displays a warning message when little RAM is left for local variables.

This can lead to program malfunctions at execution time, if local variables happen to take up more space than the remaining RAM.

How can one have the compiler detect whether local variables memory allocation will cause RAM overflow during execution or not, and warn the user at compile time only in those specific cases?

Ramanewbie
  • 145
  • 3

1 Answers1

2

You are describing "memory profiling" and is not typically a feature of C or C++. It is more common in higher level languages, such as Java, but C is a considered a much lower level language.

Typically such profiling is done at runtime using external tools such as valgrind and massif.

It's not really possible for the compiler to work out what your program will be doing when it is using external data and inputs to control how it works. Any such tests at compile time would be pretty much meaningless since any that pass in testing may still cause problems at runtime depending on what else may be happening at the same time, or even what may have happened in the past (see heap fragmentation).

Majenko
  • 105,851
  • 5
  • 82
  • 139