Re: static array initialization



In article <e9f9f827-b3ce-4ded-a329-a73b05b67385@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
mdh <mdeh@xxxxxxxxxxx> wrote:
On Jul 26, 4:19=A0pm, Barry Schwarz <schwa...@xxxxxxxx> wrote:
=A0The object has a lifetime that
spans the entire execution of the program. =A0There is only one object.
It is initialized before the program begins execution therefore it
must exist before the program begins. =A0It never ceases to exist for
the entire life of the program.

This ref is to the qualifyer "static".

Having now done the exercise, ( and about to do 5-15 through 5-17) and
peeking at the solutions provided by T&G, I would like to get a sense
from this group as to the style/correctness of declaring a static
variable ( before main, in this case) vs a non-static variable.
Briefly (in summary), the exercise is to design a function that
accepts certain flags.
The solution is to use a char, in this case "static char option =3D 0"
which stores, then provides the logic for various choices.
T&G use the syntax ( static ) for their solutions, but in the last
exercise, (5-17) which to all intents and purposes match the previous
three, use "char option =3D 0" to do the same as above.
It is my understanding that the declaration "static" guarantees the
variable will be initialize to 0. Is there something different then if
"char option =3D 0" is used. Thanks in advance.

static means different things. If you mean it's actually declared
with the static keyword, then yes, if it's not otherwise initialized
it's supposed to orchestrate it to be zero initialized. As mentioned
above, it's lifetime will also be for the lifetime of the program.
However, as you mention that the static is being declared outside
of main, it also means that "intent" is that the name being declared
does not get invovled in external identifier resolution across translation
units:

// a.c
static int i; // as if = 0

void foo();
int main()
{
foo();
}

// foo.c
static int i = 0; // different i from a.c
void foo() { ... }

If it is not declared with the keyword static, it will not be "hidden"
from external identified resolution:
// b.c
int i; // tentative definition and may eventually be = 0
// ...
foo();
// ..

// c.c
int i = 99; // actual definition, so won't be = 0
void foo() { ... }

Note that i still exists for hte lifetime of the program as well
even though not specifically declared static.

The above all said, I refer you to a really good book or
article, as unless presented altogether, most discussions
on static and such can turn into really big messes given
some of the contextual nooks and crannies, misspeaks, etc.
normally in/not in NG messages :) I've probably a few above myself.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.



Relevant Pages

  • Re: static array initialization
    ... It is initialized before the program begins execution therefore it ... Having now done the exercise, and ... The solution is to use a char, in this case "static char option = 0" ... variable will be initialize to 0. ...
    (comp.lang.c)
  • Re: question on scope of a variable
    ... >>I've tried declaring a public variable in a module of my project in hope ... >>to be able to initialize it, or have access to it's value if it's ... Yes, it is, but it seems to have a lifetime which I don't understand, and at ... some form or routine can "see" it, ...
    (microsoft.public.word.vba.general)
  • question on scope of a variable
    ... I've tried declaring a public variable in a module of my project in hope ... to be able to initialize it, or have access to it's value if it's ... While this compiles for me, I'm getting a runtime error that indicates the ... I did try declaring it using the STATIC command in the module but the ...
    (microsoft.public.word.vba.general)
  • Re: array problem
    ... Peter - I understood that in addition to declaring the array I had ... to initialize each element also. ... DefaultStyledDocument[] doc; ...
    (comp.lang.java.programmer)
  • Re: which is better practice
    ... > That's more typing. ... I like the ability to initialize in the same ... I do too, but in general, it's the declaring variables in the middle ...
    (comp.lang.perl.misc)