MHB Do Static Variables Retain Their Values Across Function Calls in Algorithms?

Click For Summary
The discussion centers on the behavior of static variables in a recursive algorithm applied to a binary tree. The algorithm increments a static integer variable, "s," each time it is called. When the function is first invoked with the root node, "s" is initialized to 0. A key point is that because "s" is declared as static, it retains its value across multiple calls to the function. Therefore, when the function is called again with a different node, "s" does not reset to 0; instead, it continues from its last value. This illustrates the concept of variable lifetime and storage duration in programming, highlighting the difference between static and auto variables. Static variables persist for the duration of the program, while auto variables exist only within their scope. The discussion also touches on the visibility semantics of static variables and the implications of other storage duration keywords like extern and register.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Smirk)

Suppose that we have an algorithm of the form:

Code:
Algorithm(NODE *P){
  static int s=0;
  Algorithm(P->RC);
  ......
  Algorithm(P->LC);
  ......
  s++;
}

where P is the root of a binary tree, for example this one:

View attachment 3616

When we call the function [m]Algorithm(a)[/m], [m]s[/m] will get the value $0$.
After that, we call the function Algorithm(b). Does [m] s [/m] get again the value $0$, or not, because of the fact that it is static? (Thinking)
 

Attachments

  • extree.png
    extree.png
    2.8 KB · Views: 84
Technology news on Phys.org
This is what is referred to as "variable lifetime" or "storage duration". An "auto" variable (the default, actually "auto" is an implicit keyword in C, and is/was in C++) is defined to exist only in the scope in which it is declared. As soon as the program execution leaves that scope, the variable ceases to exist (and a new variable will be "created" as the execution potentially reenters that scope). A "static" variable is defined to be created at the start of the program, and last until the program terminates. As such, there is only one such variable, and so the variable will persist across multiple function calls. So, yes, whatever the value of "s" was after the function returns, it will be after the function is called again.

("static" also has some semantics related to visibility - technically, in terms of storage duration, "static" is equivalent to "extern" and possibly other thread-local keywords, the point is that variables marked "static" are not "auto". this is further complicated by the existence of the "register" keyword, which represents the same storage duration as "auto" but with again a slightly different meaning; you can't take pointers to register variables)
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K