Modifications

Sauter à la navigation Sauter à la recherche
1 459 octets ajoutés ,  4 septembre 2013 à 11:36
Ligne 69 : Ligne 69 :  
== Pensez globalement. Allouez localement ==  
 
== Pensez globalement. Allouez localement ==  
 
Let's have another look at how SRAM is used (and abused):
 
Let's have another look at how SRAM is used (and abused):
 +
 +
{{ADFImage|Arduino-Memoire-41.jpg|480px}}
 +
<small>Traduction par MCHobby.be</small>
 +
 +
=== Variables Globales et Statiques ===
 +
 +
Global and Static variables are the first things loaded into SRAM. They push the start of the heap upward toward the stack '''and they will occupy this space for all eternity'''.
 +
 +
=== Dynamic Allocations ===
 +
Dynamicaly allocated objects and data cause the heap to grow toward the stack. Unlike Global and Static variables, these variables can be de-allocated to free up space. '''But this does not necessarily cause the heap to shrink!''' If there is other dynamic data above it in the heap, the top of the heap will not move. When the heap is full of holes like swiss cheese we call it a "'''fragmented heap'''".
 +
 +
=== Variables Locales ===
 +
Every function call creates a stack frame that makes the stack grow toward the heap. Each stack frame will contain:
 +
 +
* All parameters passed to the function
 +
* All local variables declared in the function.
 +
 +
This data is usable within the function, but '''the space is 100% reclaimed when the function exits'''!
 +
 +
=== Les bons trucs! ===
 +
* '''Avoid dynamic heap allocations''' - These can quickly fragment the limited heap-space.
 +
* '''Prefer local to global allocation''' - Stack variables only exist while they are being used. If you have variables that only are used in a small section of your code, consider making that code into a function and declaring the variables local to the function.
29 918

modifications

Menu de navigation