Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Question regarding static variable in c++ (http://www.velocityreviews.com/forums/t956809-question-regarding-static-variable-in-c.html)

somenath 01-23-2013 12:36 AM

Question regarding static variable in c++
 

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.


Ian Collins 01-23-2013 01:34 AM

Re: Question regarding static variable in c++
 
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

Bill Gill 01-23-2013 02:58 AM

Re: Question regarding static variable in c++
 
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


somenath 01-23-2013 03:26 PM

Re: Question regarding static variable in c++
 
On Wednesday, January 23, 2013 12:47:15 PM UTC+5:30, Paavo Helde wrote:
> somenath <somenathpal@gmail.com> wrote in
>
> news:cc92d31f-a0cc-49f8-8d37-ed5aaa6e9ade@googlegroups.com:
>
>
>
> >

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

Balog Pal 01-23-2013 08:05 PM

Re: Question regarding static variable in c++
 
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...


All times are GMT. The time now is 05:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.