Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question regarding static variable in c++

Reply
Thread Tools

Question regarding static variable in c++

 
 
somenath
Guest
Posts: n/a
 
      01-23-2013

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.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-23-2013
somenath wrote:
>
> 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.

--

Ian Collins
 
Reply With Quote
 
 
 
 
Bill Gill
Guest
Posts: n/a
 
      01-23-2013
On 1/22/2013 6:36 PM, somenath wrote:
>
> 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

 
Reply With Quote
 
somenath
Guest
Posts: n/a
 
      01-23-2013
On Wednesday, January 23, 2013 12:47:15 PM UTC+5:30, Paavo Helde wrote:
> somenath <(E-Mail Removed)> wrote in
>
> news:(E-Mail Removed):
>
>
>
> >

>
> > 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();

>
>
>
> 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.
>
>
>
> > cout<<"Val is: " <<val<<endl;

>
> > }

>
> > return 0;

>
> > }

>
> >

>
> > Here the "val" variable is initialized multiple times.

>
>
>
> No, it is not, it just looks so.
>
>
>
> > 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.

>
>
>
> 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?
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      01-23-2013
On 1/23/2013 1:36 AM, somenath wrote:
> 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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java question about static variable in static method Aakash Java 3 01-19-2008 12:34 PM
non-static variable this cannot be referenced from a static context kookey Java 3 08-20-2005 07:43 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
Global static variable vs static method Marcin Vorbrodt C++ 3 09-05-2003 07:52 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments