Pages: [1]   Go Down
Author Topic: Declaring variables && destroying them to free some RAM ... ?!  (Read 461 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25816
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void do_stuff(int i) {
  int x, y;  // these are freed
  static int a,b; // these are not freed
  // so stuff
}
Logged

"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.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25816
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: