Question regarding static variable in c++

S

somenath

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?

#include<iostream>
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.
 
I

Ian Collins

somenath said:
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?

Valid in what sense?
#include<iostream>
using namespace std;

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

Try adding a line like

cout << "Initialising" << endl;

here to see what's going on.
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;
}

Your "char*" variables should be "const char*".
Here the "val" variable is initialized multiple times.

Is it? See above.
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?

No matter what you try, it is only initialised once.
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.

The logic must be different.

PS: Please wrap your lines.
 
B

Bill Gill

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?

#include<iostream>
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.
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
 
S

somenath

Here you have 2 statics, where one is initialized from the other.

Strictly speaking, this should not be necessary, one static should be

enough to hold a long-living value. Now you have 2 long-living duplicates

with the same value. But as written, this is not an error either.






No, it is not, it just looks so.











Namespace level static initialization order fiasco? Multithreading?

Neither is present in your example code so of course it runs as expected.



The empty string probably comes from a static initialization step, which

means that a static is used before the proper dynamic initialization is

completed. Look up static and dynamic initialization in C++.

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?
 
B

Balog Pal

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.

No it isn't. It is initializes exatly once.
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?

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...
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top