reset/purged static variables in the C/C++ function

How can we reset/purged static variables in the C/C++ function on certain condition met?
A static variable is "just" a variable that exists for the lifetime of the whole program. You just set their value like any other variable.


1
2
3
4
5
static int x;
if (some_condion)
{
  x = 7;
}


As for "purging" a variable; that doesn't make much sense in this context. What do you mean by "purging" a variable?
If by "reset/purged static variable" you mean permanently removing variable, then you might consider declaring a static pointer to heap. and deleting it when no longer needed.

otherwise it' not possible to turn static variable into automatic storage variable (stack variable). these are 2 different storage types. you choose either one or the other.

edit:
well both are stack variables, (static or non static), only destruction takes time at different time.
Last edited on
well both are stack variables, (static or non static), only destruction takes time at different time.
I supposed the runtime environment could put static or global variables on the stack, but all the systems I'm aware of don't do that. They are placed in a separate data space.
@dhayden, thanks for pointing out. this is something I never learned much.
Topic archived. No new replies allowed.