Static Variables: Are They Really Static?

  • Thread starter Thread starter ptabor
  • Start date Start date
  • Tags Tags
    Static Variables
Click For Summary

Discussion Overview

The discussion revolves around the behavior of static variables in C++ functions, particularly in the context of initialization and persistence across multiple function calls. Participants explore the implications of using static variables within a function, especially when compiled in a multi-process environment using MPI.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue where a static variable, intended to retain its value across function calls, is reset to zero on subsequent calls, raising questions about the expected behavior of static variables.
  • Another participant expresses confusion about the declaration of static variables within a function, questioning whether this is permissible and suggesting an alternative approach.
  • It is noted that static variables are initialized to zero by default unless explicitly initialized, with a suggestion that the compiler may be re-initializing the variable incorrectly.
  • A participant points out that the use of MPI may lead to multiple initializations across different processes, which could explain the observed behavior if the "first time through" condition is only valid for one process.
  • One participant requests more information about the code and the context of its execution, including whether the program is multithreaded and how the first invocation is determined.
  • A code example is provided that demonstrates the expected behavior of static variables, showing that they retain their values across calls when initialized correctly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the issue with static variables, and multiple competing views regarding their behavior and proper usage remain. The discussion highlights uncertainty about the interaction between static variables and MPI, as well as the implications of multithreading.

Contextual Notes

Limitations include the lack of visibility into the complete code and the specific conditions under which the function is called, as well as potential issues related to multithreading and process management in MPI.

ptabor
Messages
14
Reaction score
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
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?
 
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.
 
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 (Часлав Илић)
 
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?
 
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
6
Views
2K