user20088: 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?

user4143: 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).

20088: <p>Upon compiling a sketch using the Arduino IDE, a message like the following gets displayed in the console:</p>
<pre><code>Global variables use 1540 bytes (75%) of dynamic memory, leaving 508 bytes for local variables. Maximum is 2048 bytes.
</code></pre>
<p>In addition, it seems that if the global variables use exceeds around 75% of total dynamic memory, the following message gets displayed in addition:</p>
<pre><code>Low memory available, stability problems may occur.
</code></pre>
<p>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.</p>
<p>This can lead to program malfunctions at execution time, if local variables happen to take up more space than the remaining RAM.</p>
<p>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?</p>
 4143: <p>You are describing &quot;memory profiling&quot; 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.</p>
<p>Typically such profiling is done at runtime using external tools such as <code>valgrind</code> and <code>massif</code>.</p>
<p>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 <a href="https://cpp4arduino.com/2018/11/06/what-is-heap-fragmentation.html" rel="nofollow noreferrer">heap fragmentation</a>).</p>
