Exploring Static Variables in Algorithms

In summary, the function Algorithm(P) iterates through a binary tree starting from the root node, with a static variable s that persists throughout the program's execution. This means that the value of s will not be reset to 0 when the function is called again, and it will retain its previous value.
  • #1
evinda
Gold Member
MHB
3,836
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: 59
Technology news on Phys.org
  • #2
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)
 

FAQ: Exploring Static Variables in Algorithms

What are static variables in algorithms?

Static variables in algorithms refer to variables that retain their value throughout the entire run of a program. They are not reset or re-initialized when a function is called or when a block of code is executed.

Why are static variables used in algorithms?

Static variables are used in algorithms because they provide a way to store and access a value across multiple function calls or executions of a code block. This can be useful for keeping track of important information or for optimizing performance.

How are static variables different from regular variables in algorithms?

Unlike regular variables, static variables are only initialized once and retain their value throughout the entire run of the program. Regular variables are initialized each time a function is called or a code block is executed.

What are the advantages of using static variables in algorithms?

The main advantage of using static variables in algorithms is that they can help improve efficiency and performance. Since they are not re-initialized each time a function is called or a code block is executed, they can save time and memory.

Are there any limitations or drawbacks to using static variables in algorithms?

One limitation of using static variables in algorithms is that they are shared across all instances of a class or function. This can lead to unexpected behavior if not used carefully. Additionally, since they retain their value throughout the entire run of a program, they can make debugging more difficult.

Similar threads

Replies
1
Views
1K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
13
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
4
Views
2K
Back
Top