[Icc-avr] Freeing static variables

David Brown david_brown at hotpop.com
Mon Oct 22 02:26:51 PDT 2007


By their very definition, "static" variables are allocated a fixed 
address in memory by the linker, and cannot be "freed".  You have to 
figure out why you might want to re-use the space they take - if you are 
using heap-allocated dynamic memory (a terrible idea on most embedded 
systems!), for example, then you could use malloc and free for the 
calibration data.  If you need it for other specific purposes after 
calibration, you could do something like define a union containing a 
calibration data structure and a run-time data structure, as long as you 
are careful to track which one is valid.

mvh.,

David


Bengt Ragnemalm wrote:
> Is there any intelligent way to release the memory used by a static 
> variable?
> 
>  
> 
> I have a calibration routine that is run the first loops of the program. 
> If the calibration was done in a separate function , I could just use 
> temporary variables but as this is run in the entire normal program loop 
> I can not see any other way than to use statics for my temporary 
> variables in the calibration routine.
> 
>  
> 
> But after the calibrating is done, it would have been nice to free the 
> space used by the static variables.
> 
>  
> 
> Maybe there is some better way to do this so any suggestions are welcome.
> 
>  
> 
> Regards,
> 
> Bengt
> 




More information about the Icc-avr mailing list