title: How to minimize memory allocation on embedded platforms?
body: <p>How do you minimize dynamic memory allocation in C++?</p>
  
  <p>I'm writing some sketches for an Arduino Mega, and on occasion it's hanging, and I suspect it's suffering from memory fragmentation. However, I'm a little unclear as to when variables are allocated.</p>
  
  <p>If I have a class with a method <code>do_stuff</code> like:</p>
  
  <pre><code>class MyController{
  
  public:
      MyController(){
          ...init...
      }
  
      void do_stuff(){
          int value = 123;
      }
  };
  </code></pre>
  
  <p>I instantiate <code>MyController</code> once but then execute <code>do_stuff()</code> multiple times. Does the program dynamically allocate <code>value</code> each time I call the method or just once when I instantiate the class?</p>
  
  <p>If the former, would it be better to change the method variable to a class variable, so it's only allocated once, and doesn't risk fragmenting the heap?</p>