Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Static Variables

  1. Jun 1, 2007 #1
    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.
     
  2. jcsd
  3. Jun 1, 2007 #2
    Just out of curiosity how can you have a static variable declared within a method? Tought that is not possible.

    Code (Text):

    function void something(...){

    static int xyz = ...;

    }
     
    Shouldn't one do

    Code (Text):

    static int xyz = ....

    function void something(...){

    xyz = ....;

    }
     
    instead?
     
  4. Jun 2, 2007 #3

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  5. Jun 2, 2007 #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 (Часлав Илић)
     
  6. Jun 2, 2007 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
     
  7. Jun 2, 2007 #6

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    The following behaves correctly (I think) using Borland C:

    Code (Text):

    #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: Jun 2, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Static Variables
  1. C# oop static instance (Replies: 2)

  2. Global variables. (Replies: 7)

  3. Valid variable names (Replies: 4)

Loading...