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: 91
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)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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