Go Down

Topic: Declaring variables && destroying them to free some RAM ... ?! (Read 530 times) previous topic - next topic

simkard

Hi all,

I'm actually gathering a lot of code on a project and needs some way to free some more RAM like i already did !
How is it possible to declare some variables while in a function, and, then, when the functions ends, destroy the variables in RAM to free some precious Bytes ?

Thanks !

sbr_

Local variables in functions only live as long as the function is executing -- they are "created" upon entering and "destroyed" upon exiting the function. With:

Quote

void do_stuff(int i) {
  int x, y;
  // so stuff
}


'i', 'x', and 'y' only live during a call to 'do_stuff'. Hence what you wish is the normal behaviour already...

simkard

In fact, i already user this kind of behavior for the variables but i was wondering if there was another way to really destroys them from RAM.

For example :
Let's say i have a function that consumes 7kB of RAM and another one that consumes the same size.
Is there a way to execute the 1st function, then, free the RAM of the 1st function variables, then, executes the 2nd function, and, free the RAM of the 2nd function variables.

I think this mustn't be probable because as i understand the compiler, it reserves the size of every variable defined in the whole sketch on a one time basis.

AWOL

Quote
Local variables in functions only live as long as the function is executing

Local non-static variables in functions only live as long as the function is executing.

These variables are called "automatics", and live on the stack.
Static local variables live with the globals, and are not freed when the function exits.
Code: [Select]
void do_stuff(int i) {
 int x, y;  // these are freed
 static int a,b; // these are not freed
 // so stuff
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

Quote
Let's say i have a function that consumes 7kB of RAM and another one that consumes the same size.

If you have a big enough stack, then the mechanism is exactly as described above.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up