initialization of local static variable

  • Thread starter V.Subramanian, India
  • Start date
P

Philip Lantz

It really requires two different symbols:

'=' to mean dynamic assignment
'==' (perhaps) to mean one-time initialisation

The trouble is the current usage in C uses '=' for both meanings.

C does have different syntax to mean dynamic assignment vs. one-time
initialization. They do both use the '=' symbol, but they are easily
distinguished, both by the compiler and the programmer.

My 2 cents worth:
I definitely do not think that extending initialization of statics to
allow non-constant expressions should also imply that they should be
"initialized" more than once; that would completely ignore the concept
of "initialization".

If you want to assign a value to a static variable, use assignment!
Don't try to use initialization for a role it doesn't have.

Of course, as others have pointed out, Jacob is free to implement
this extension in the way that he thinks is most useful to his
users. This is just my opinion. I would also encourage the generation
of a warning message for this construct, unless the user explicitly
enables the feature.

Philip
 
N

Nobody

Well, why shouldn't be possible?

I don't see any reason. The compiler can do it, the language
looks the same, why shouldn't I do it?

Besides the obvious (the standard doesn't like it) is there
any reason not to do it?

Because it should report an error. The programmer is trying to do
something that isn't meaningful; the compiler should ideally try to warn
them, rather than (metaphorically) sniggering to itself, opening the
popcorn, and watching the programmer try to figure it out on their own.
Now, it *could* be said that it would be unexpected because
static initializations are done at load time but obviously
the intention of the programmer here can't be that since
there is no way a function can be called at load time...

You're expecting too much from the programmer. E.g. assuming that they
actually understand the difference between initialisation and assignment.
Given how long you've been here, you really should know better.

Just because something is possible, that doesn't mean that it's a good
idea. E.g. if you're writing a text editor, it's quite simple to add
an option to allow tab stops to be set at something other than every eight
columns. Dealing with the consequences of such a feature is a) somewhat
less simple and b) Somebody Else's Problem (TM).

More generally, any kind of extension creates a risk of "Embrace, Extend,
Extinguish", even if that wasn't the original intention. In some cases,
the drawbacks may be outweighed by the utility. This doesn't appear to be
such a case.
 
B

Ben Bacarisse

Nobody said:
Because it should report an error. The programmer is trying to do
something that isn't meaningful; the compiler should ideally try to warn
them, rather than (metaphorically) sniggering to itself, opening the
popcorn, and watching the programmer try to figure it out on their
own.

What's no meaningful about it? I agree that the actual meaning that
Jacob has chosen is odd (and I think, in retrospect, he does too) but
the idea of permitting new ways to initialise a static object is not, to
me, inherently meaningless.

<snip stuff the I largely agree with>
 
N

Nobody

What's no meaningful about it? I agree that the actual meaning that
Jacob has chosen is odd (and I think, in retrospect, he does too) but
the idea of permitting new ways to initialise a static object is not, to
me, inherently meaningless.

An initialisation is not an assignment. An initialisation sets the *initial*
value of the object at the point that it comes into existence. For a
static object, that point is program start, before main() is entered. At
that point, no automatic variables exist, so initialising a static object
from an automatic variable is meaningless.

An assignment sets the value of the object at the point that the
assignment statement was executed. But an initialisation isn't a
statement, and isn't "executed". For automatic variables, the distinction
is largely academic, but it's quite significant for static variables.

Sure, you can create a language where this is meaningful, but such a
language differs from C in a rather fundamental way.
 
B

Ben Bacarisse

Nobody said:
An initialisation is not an assignment. An initialisation sets the *initial*
value of the object at the point that it comes into existence. For a
static object, that point is program start, before main() is entered. At
that point, no automatic variables exist, so initialising a static object
from an automatic variable is meaningless.

That was not the original example. I was going to say I did not spot
the change to talking about an automatic variable, but I just had a
quick look and this sub-thread seems to be all about the original
code!
An assignment sets the value of the object at the point that the
assignment statement was executed. But an initialisation isn't a
statement, and isn't "executed". For automatic variables, the distinction
is largely academic, but it's quite significant for static variables.

Nit (not germane to the point)... Initialisations can be executed (at
least that a good way to think of some of them) and it really does
matter for automatic variables. If int i = f(x) + g(y); is not executed
it can't mean anything useful.
Sure, you can create a language where this is meaningful, but such a
language differs from C in a rather fundamental way.

Given that you are talking about initialising a static with an automatic
variable, I am having trouble imagining it in any language!

To me, the crucial differences between initialisation and assignment are
(a) initialisation works for non-assignable objects (like const vars)
and (b) it happens once at the start of the object's lifetime. Any
change to the value after that is some form of assignment. I like to
think of both as being executed (even for static objects) -- but that's
just for simplicity of exposition.
 
B

Barry Schwarz

snip
so this for a is not 'a' initialization:

void f(void){int a; ....}

because no one sets "the *initial* value to" 'a'
even if f() is called

It is not an initialization because initialization requires an
assignment expression which is clearly not present (6.7.8).

snip
so this below, for 'a', is initialzation of one assignment?

void f(void){int a=3; ....}

i think one initialization

Is this legal C90/C99 or is it something added to the new draft>
 
J

James Kuyper

....
Is this legal C90/C99 or is it something added to the new draft>

I think you may have been confused by the whitespace (or lack thereof).
The only non-C feature is ...., representing the missing rest of the
body of the function. Here's the same, with a little extra white space:

void f(void)
{
int a = 3;
....
}
 
J

Jens Gustedt

Hi,

Am 10/08/2011 01:53 AM, schrieb jacob navia:
Le 08/10/11 00:56, Ian Collins a écrit :

Yes, you are right. I think that is a heavy argument in favor of doing
it just once.

Another argument of doing it like C++ is that it really enhances the
semantics of static variables. The way you currently seem to have it can
be easily emulated in standard C. Just use a definition without
initialization that is followed by an assigment.

Having "once initialized" static variables are a real advantage if this
is guaranteed to be thread safe (in some former version of C++ that I
had to deal with it wasn't). And it also plays well if you declare the
variable "const".

Jens
 
B

Barry Schwarz

I think you may have been confused by the whitespace (or lack thereof).
The only non-C feature is ...., representing the missing rest of the
body of the function. Here's the same, with a little extra white space:

void f(void)
{
int a = 3;
....
}
Yup. I managed to simultaneously skip over the (void) and replace the
{ with (. Gotta learn to read slower.
 
I

Ike Naar

This can be surprisingly complicated in the presence of threads. This
is what g++ has to do:

double pi()
{
static const int p = 4 * atan (1);

int p ? Are you in the "pi equals three" camp?
 
G

Geoff

so for you the global variable "vii"
...
static int vii;
...
is not initilizated...
for me it is initializated to 0 even if there is no "vii=number;"

This behavior depends on the target environment and whether it is a
debug build, release build or the optimization level when it was
compiled. It can also depend on whether the application is being run
in a debugger or not. There is no guarantee that a variable that is
not explicitly initialized will have any particular value when
accessed. You cannot depend on this behavior. A programmer who does is
in for some unpleasant surprises.
 
I

Ike Naar

This behavior depends on the target environment and whether it is a
debug build, release build or the optimization level when it was
compiled. It can also depend on whether the application is being run
in a debugger or not. There is no guarantee that a variable that is
not explicitly initialized will have any particular value when
accessed. You cannot depend on this behavior. A programmer who does is
in for some unpleasant surprises.

A static variable that is not explicitly initialized is guaranteed
to be initialized to zero.
 
J

James Kuyper

....
... There is no guarantee that a variable that is
not explicitly initialized will have any particular value when
accessed.

Yes, there is, in section 6.7.8p10:
 
B

Barry Schwarz

so for you the global variable "vii"
...
static int vii;
...
is not initilizated...
for me it is initializated to 0 even if there is no "vii=number;"

I'm sorry; I didn't realize you wanted to play language lawyer. In
that case, I have to ask: what difference does it make if vii is
global or has block scope?
 
K

Keith Thompson

io_x said:
each algo when has to deal with arrays and numbers
has to have one base written in assembly
it is no matter what language what the remain of that algo
is written

That is complete nonsense.
 
K

Keith Thompson

Keith Thompson said:
That is complete nonsense.

I suppose I should expand on that.

It's perfectly reasonable for algorithms dealing with arrays and numbers
to be expressed in some high-level language, or even in pseudo-code (if
the point is to communicate the algorithm to a human reader). There is
rarely any need (or even advantage) to expressing part of such an
algorithm in assembly language.

There are some cases where assembly language can be useful to speed up
portions of an algorithm, but that's far from universal -- and it comes
with substantial costs in clarity and portability.
 
B

Barry Schwarz

Barry Schwarz said:
I'm sorry; I didn't realize you wanted to play language lawyer. In
that case, I have to ask: what difference does it make if vii is
global or has block scope?

Buon Giorno
if "int vii;" and "vii" is gloabal
[ie "int vii;" out of all functions or procedure]
the compiler i use assign the initial value 0
to that variable [vii==0]

if "int vii;" is in a function or procedure
or in its "block scope" than "vii" has one value

All scalar variables have only one value.

The question I asked you was what is different about the
initialization of static int vii when it is at global scope compared
to when it is within a function?
i don't know in the range of int...
at last on what rememeber

What does the range of values for an int have to do with how it is
initialized by default? Why are you introducing extraneous and
irrelevant side issues into the discussion?
 
B

Barry Schwarz

io_x said:
Barry Schwarz said:
"Barry Schwarz" <[email protected]> ha scritto nel messaggio

snip

so this for a is not 'a' initialization:

void f(void){int a; ....}

because no one sets "the *initial* value to" 'a'
even if f() is called

It is not an initialization because initialization requires an
assignment expression which is clearly not present (6.7.8).

so for you the global variable "vii"
...
static int vii;
...
is not initilizated...
for me it is initializated to 0 even if there is no "vii=number;"

I'm sorry; I didn't realize you wanted to play language lawyer. In
that case, I have to ask: what difference does it make if vii is
global or has block scope?

Buon Giorno
if "int vii;" and "vii" is gloabal
[ie "int vii;" out of all functions or procedure]
the compiler i use assign the initial value 0
to that variable [vii==0]

that vii=0
as say here someone: first of the first main() instruction

What main()? The only function in your message was f().
if "int vii;" is in a function or procedure
or in its "block scope" than "vii" has one value
i don't know in the range of int...
at last on what rememeber

and this ["vii" has one start value i don't know]
always, each time i call the function

A static variable always contains the last value it was assigned or
its default initialization if it has not been assigned. It's your
code. If you don't know what value, if any, you assigned it, we can't
help.
 
B

Barry Schwarz

io_x said:
Barry Schwarz said:
"Barry Schwarz" <[email protected]> ha scritto nel messaggio

snip

so this for a is not 'a' initialization:

void f(void){int a; ....}

because no one sets "the *initial* value to" 'a'
even if f() is called

It is not an initialization because initialization requires an
assignment expression which is clearly not present (6.7.8).

so for you the global variable "vii"
...
static int vii;
...
is not initilizated...
for me it is initializated to 0 even if there is no "vii=number;"

I'm sorry; I didn't realize you wanted to play language lawyer. In
that case, I have to ask: what difference does it make if vii is
global or has block scope?

Buon Giorno
if "int vii;" and "vii" is gloabal
[ie "int vii;" out of all functions or procedure]
the compiler i use assign the initial value 0
to that variable [vii==0]

if "int vii;" is in a function or procedure
or in its "block scope" than "vii" has one value
i don't know in the range of int...
at last on what rememeber

i remember K&R speak about that too

yes i find K&R seems to speak about
static variable initialization
[global or in a function or block as "static int etc;"]
in A10.2
"Se nell'unità di traduzione non compaiono definizioni dell'oggetto,
tutte le definizioni sperimentali relative a esso diventano
un'unica definizione con inizializzatore 0"

Since A10.2 talks about external declarations, why would you even
bring it up in this discussion? You conveniently left of the
introductory sentence to your quote: "For objects with external
linkage ..." It has absolutely nothing to do with the default
initialization of a variable declared with the static storage-class.
This is just another red herring.
 
S

Seebs

An intrepid programmer can program in Fortran no matter what
language they use.

I recently spent quite a while tracking down a bug.

What finally revealed to me what was happening was:

printf("%f\n", 3.0);
which, of course, yielded
0.000000

-s
 

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

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,589
Members
47,212
Latest member
JaydenBail

Latest Threads

Top