Static Variables: Are They Really Static?

  • Thread starter Thread starter ptabor
  • Start date Start date
  • Tags Tags
    Static Variables
AI Thread Summary
Static variables in C++ are intended to retain their values between function calls, but issues can arise, especially in multi-process environments like MPI. The observed behavior of the static array being reset to zeros on subsequent calls may be due to the initialization logic or the way processes are managed in MPI, where each process may treat the first invocation differently. It is suggested to verify the "first time through" condition and consider whether the program is multithreaded, as race conditions could affect variable states. Moving static variables outside the function is not recommended due to potential global clutter, but it may help in troubleshooting the issue. Properly implemented, static variables should not be reinitialized on each call, indicating a possible underlying problem in the code structure or execution context.
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:
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.

Similar threads

Back
Top