Static Variables: Are They Really Static?

In summary, the conversation discussed the use of static variables within a function and the behavior observed when the function is called multiple times. It was noted that static variables are initialized to zero by default, unless explicitly initialized in the declaration statement. It was suggested that the issue could be caused by the compiler generating code to re-zero out the static variables each time the function is called, and it was recommended to try moving the static variable(s) outside the function. There was also a mention of potential issues with multithreading and race conditions. An example code was provided that exhibited the correct behavior when using a Borland C compiler.
  • #1
ptabor
15
0
I am doing some work using static variables - and they apparently aren't static.

void function ( arguments ) {

static float Y_kn[101][7];

if (first time through) {
initialize Y_kn
return;
}
use Y_kn to make some calculations
}

this function is called many many times (hundreds) and for the first time through Y_kn gets initialized properly. On subsequent calls the array is set to all zeros. I could initialize it every time I go through, but I thought that was the point of static variables - initialize them once and they keep the same values for the duration of the program.

This is being compiled on a unix system using mpiCC


Perhaps someone can provide some insight, because I'm stumped.
 
Technology news on Phys.org
  • #2
Just out of curiosity how can you have a static variable declared within a method? Tought that is not possible.

Code:
function void something(...){

static int xyz = ...;

}

Shouldn't one do

Code:
static int xyz = ...

function void something(...){

xyz = ...;

}
instead?
 
  • #3
Static variables are initialized to zero by default, unless explicitly initialized in the declaration statement. What is happening in your case is the compiler is generating code to re-zero out the static variables each time the function is called. I'm not sure what the "proper" behavior for static variables inside a function is. As mentioned, try moving the static variable(s) outside the function and they should only get zeroed (or initialized) at startup time.
 
  • #4
Given that you are using MPI, it could be that the "first time through" condition is only valid once in one process, whereas you have as many default initializations as there are processes. Is the problem persisting if you use only one process?

In any case, conforming C++ compiler should not reinitialize to zeros on each function invocation, and it is generally not a good idea to put the variable outside (cluttering the global level).

--
Chusslove Illich (Часлав Илић)
 
  • #5
It's hard to tell what you did wrong without seeing the code. Could you make a small example that exhibits the behavior you're seeing, and post it to the forum?

Is your program multithreaded? Have you safeguarded against race conditions? How are you determining when it's the first time through, and when it's not?
 
  • #6
The following behaves correctly (I think) using Borland C:

Code:
#include <iostream>
using namespace std;

void f(bool first)
{
	static int x;

	if (first) 
		x = 123;
	cout << x << '\n';
}

void main()
{
	f(false);
	f(true);
	f(false);
	f(true);
}

The output being
0
123
123
123
 
Last edited:

1. What is a static variable?

A static variable is a variable that is associated with a class rather than with an instance of that class. This means that all instances of the class share the same static variable value, and changes to the variable will affect all instances.

2. How is a static variable different from a regular variable?

A regular variable is associated with a specific instance of a class and has a different value for each instance. A static variable is associated with the class itself and has the same value for all instances of the class.

3. When should I use a static variable?

Static variables are useful for storing information or values that are shared among all instances of a class. They can also be used to keep track of certain data that needs to be accessible globally throughout the program.

4. Can I change the value of a static variable?

Yes, the value of a static variable can be changed. However, since it is shared among all instances of the class, the change will affect all instances and may cause unexpected behavior in the program.

5. Are static variables really static?

The term "static" can be misleading. While the value of a static variable remains consistent throughout the program, it is not necessarily "static" in the traditional sense. The value can be changed and can also be affected by other factors such as inheritance and polymorphism.

Similar threads

  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
13
Views
877
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
6
Views
916
  • Programming and Computer Science
Replies
8
Views
848
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top