Topic: Q: static var initialization


Author: AllanW@my-dejanews.com
Date: 1998/12/02
Raw View
In article <740qsu$bfc$1@nnrp1.dejanews.com>,
  rado42@my-dejanews.com wrote:
>
> void f (int p)
> {
> static int s = p;
> //...etc
> }
>
> 1. Is this standard code? (VC6 compiles it)

Yes, absolutely legal code.

> 2. If yes, what is the behaviour (VC6 initializes 's' on the 1st call
> to f() only)

That is correct behavior. In fact, that's what you are asking for
when you use the "static" keyword.

Why is s static? Are you returning the address, or something
similar? If so, you may be able to get what you want by using
assignment instead of initialization:
    // int s = p; // Remove this, and replace with:
    int s;
    s = p;

> 3. Is it the same with C and C++?

I no longer remember if it's legal C. If it is, then certainly it will
have the same results. Try running VC6 in "C" mode -- I'm sure they
got this one right.

> To me this looks quite buggy. I had a bug recently, and the reason was
> the 'smartness' of the compiler, from which I expect error message in such
> case.

Compilers are not allowed to flag legal code as being an error. The
closest they can do is issue a warning and then continue compiling.
For instance:
    void foo(int i) {
        if (i=0) { /* ... */ }
    }
Technically this is legal in C++. It's well defined and predictable,
but probably not what you intended. Most compilers will issue a
warning, but all compilers will compile the code.

However, the case of the static int with a non-const initializer
is completely legal and does not match any common error patterns.
There is no reason for the compiler to spit out any message at all.
It could well be buggy, but the bug is in your algorithm, not in
your compiler.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1998/12/02
Raw View
AllanW@my-dejanews.com wrote in message <74202o$crj$1@nnrp1.dejanews.com>...
>
>In article <740qsu$bfc$1@nnrp1.dejanews.com>,
>  rado42@my-dejanews.com wrote:
>>
>> void f (int p)
>> {
>> static int s = p;
>> //...etc
>> }
>>

>> 3. Is it the same with C and C++?
>
>I no longer remember if it's legal C. If it is, then certainly it will
>have the same results. Try running VC6 in "C" mode -- I'm sure they
>got this one right.


*** No, it's not legal C. In C statics can be initialized with compile-time
constants.

>> To me this looks quite buggy. I had a bug recently, and the reason was
>> the 'smartness' of the compiler, from which I expect error message in
such
>> case.


*** This feature of initializing statics with dynamic values is powerful.
With all due respect, I think the bug was only because of you not knowing
exactly what static does.

Andrei




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: rado42@my-dejanews.com
Date: 1998/12/01
Raw View
Hi all,
I've a question:

void f (int p)
{
static int s = p;
//...etc
}

1. Is this standard code? (VC6 compiles it)

2. If yes, what is the behaviour (VC6 initializes 's' on the 1st call
to f() only)

3. Is it the same with C and C++?


To me this looks quite buggy. I had a bug recently, and the reason was
the 'smartness' of the compiler, from which I expect error message in such
case.

Regards,
Rado


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/12/01
Raw View
rado42@my-dejanews.com writes:

>void f (int p)
>{
>static int s = p;
>//...etc
>}

>1. Is this standard code? (VC6 compiles it)

>2. If yes, what is the behaviour (VC6 initializes 's' on the 1st call
>to f() only)

>3. Is it the same with C and C++?

Perfectly valid in C and C++. By definition, a local static variable
is intialized the first time control passes through its definition.
The first time function f is called, the local s gets the value of
parameter p.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]