Question regarding static variable in c++

Discussion in 'C++' started by somenath, Jan 23, 2013.

  1. somenath

    somenath Guest

    I have one question regarding the static variable in C++.
    According to my understanding the static variable is initialized only once.So is the following program valid?

    using namespace std;

    static char* getVal()
    static char *s ="test";
    return s;

    int main (void)
    int i =0;
    for (i =0;i< 1000;i++)
    static char *val = getVal();
    cout<<"Val is: " <<val<<endl;
    return 0;

    Here the "val" variable is initialized multiple times. Though it is printing the correct output every time I run this program but is this behavior (trying to initialized static variable multiple times) well defined?

    The reason for asking this question is ,the similar logic does not provide correct result all the time when it is part of large program. There, the "val" variable sometime gets empty string.

    I tried to get answer by looking into the document “n1905” but did not get any help.
    somenath, Jan 23, 2013
    1. Advertisements

  2. somenath

    Ian Collins Guest

    once. So is the following program valid?

    Valid in what sense?
    Try adding a line like

    cout << "Initialising" << endl;

    here to see what's going on.
    Your "char*" variables should be "const char*".
    Is it? See above.
    times) well defined?

    No matter what you try, it is only initialised once.
    The logic must be different.

    PS: Please wrap your lines.
    Ian Collins, Jan 23, 2013
    1. Advertisements

  3. somenath

    Bill Gill Guest

    As I understand the matter the 'static' keyword in this context
    causes "val" to retain the last value that was set when the
    program exits the code block.

    The first time you run the loop and exit the loop then the next time
    that you run the loop "val" will already have a value in it. Normally
    since you are reading getVal() before using it then the program
    should run as expected. However I would not use 'static' in this place
    unless you want to start all runs after the first to already have
    "val" initialized.

    Bill Gill
    Bill Gill, Jan 23, 2013
  4. somenath

    somenath Guest

    Many thank all of you for the response.

    Yes the large program is multithreaded. But I am almost sure that is
    not multi-threading issue.
    I am not aware of “Namespace level static initialization order fiasco”
    also not aware of “static and dynamic initialization” in C++.
    I will try to learn it. If you could provide some more details it
    will be very helpful.
    Is “static and dynamic initialization” is not present in C?
    somenath, Jan 23, 2013
  5. somenath

    Balog Pal Guest

    No it isn't. It is initializes exatly once.
    You can't initialize any kind of object more than once. Though you may
    mutate the value, like assign something else.

    for (int i=0; i<4; ++i)
    static int val = i;
    // val = i;
    cout << val;
    will print 0000, but uncommenting the assignment will print 0123...
    Balog Pal, Jan 23, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.