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

AI Thread 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: 80
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)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top