memory-saving

 

Question 1.

(A constant) vs (an initialized local var of the same type): which is more memory-saving ?

#define pi 3.14

vs

int start()
    {
     double pi=3.14;
     ...
    }

Question 2.

Declaring a var inside (if) or (for) or (while) vs declaring it outside. Which case is more memory-saving ?


Question 3.

Please compare 3 following styles of coding:

// style (1)

while ( a()*b() != 0 ) { do something }

// style (2)

while (true) { if ( a()*b()==0 )  break; do something }

// style (3)

while (true) { if (a()==0) break; if (b()==0) break; do something }

//-------------------------------------------------------------------------

which style above is the most memory-saving style of coding ?

I am looking forward to hearing advices from experts. Thanks.

 
  1. Depends on how many times used
    There are two copies
    #define pi 3.14 // no memory at all
    #define pi 3.14
    function(pi); // One copy
    #define pi 3.14
    function1(pi); // One copy
    function2(pi); // Probably another copy
    double pi=3.14;
    But you are doing the assignment each tick.

  2. Declaring a var inside (if) or (for) or (while) vs declaring it outside. Which case is more memory-saving ?
    No difference. You save an assignment to zero with assignment while declaring.
  3. which style above is the most memory-saving style of coding ?
    No difference. You save a function call to b() with #3
 
Is there difference for RTos and OS? ;)
Reason: