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

Nov 18, 2019 at 9:18am
How can we reset/purged static variables in the C/C++ function on certain condition met?
Nov 18, 2019 at 9:26am
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?
Nov 18, 2019 at 3:00pm
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 Nov 18, 2019 at 3:04pm
Nov 18, 2019 at 9:35pm
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.
Nov 19, 2019 at 11:09pm
@dhayden, thanks for pointing out. this is something I never learned much.
Topic archived. No new replies allowed.