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

Click For Summary
SUMMARY

The discussion clarifies that static variables in C and C++ retain their values across multiple function calls. Specifically, the variable 's' in the provided algorithm remains persistent due to its static declaration. This means that when the function Algorithm is called multiple times, the value of 's' does not reset to zero but continues from its last value. The conversation also highlights the distinction between static and auto variables, emphasizing that static variables exist for the duration of the program, while auto variables are limited to their scope.

PREREQUISITES
  • Understanding of C and C++ programming languages
  • Familiarity with variable storage duration concepts
  • Knowledge of binary tree data structures
  • Basic grasp of function call mechanics in programming
NEXT STEPS
  • Study the implications of static variables in C and C++ programming
  • Learn about variable lifetime and storage duration in depth
  • Explore the differences between static, auto, and register variables
  • Investigate the behavior of recursive functions with static variables
USEFUL FOR

Software developers, particularly those working with C and C++, educators teaching programming concepts, and anyone interested in understanding variable lifetime and function behavior in algorithms.

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: 94
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)
 

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