[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Array definition guarantees

drM

8/13/2008 2:36:00 AM

May I ask.

If an array is defined , not as a static, but outside of a function,
is there any guarantee as to the contents of each element?

Thanks.
26 Answers

s0suk3

8/13/2008 2:51:00 AM

0

On Aug 12, 9:36 pm, mdh <mdeh@comcast.net> wrote:
> May I ask.
>
> If an array is defined , not as a static, but outside of a function,
> is there any guarantee as to the contents of each element?
>

A variable (of any kind, including an array) declared outside a
function always has static storage duration, and declaring such a
variable with the 'static' storage class makes it have internal
linkage. So yes, an array declared outside a function is guaranteed
have all its elements set to zero.

Sebastian

drM

8/13/2008 2:56:00 AM

0

On Aug 12, 7:51 pm, s0s...@gmail.com wrote:
> On Aug 12, 9:36 pm, mdh <m...@comcast.net> wrote:
>
> >
>
> > If an array is defined , not as a static, but outside of a function,
> > is there any guarantee as to the contents of each element?
>
> A variable (of any kind, including an array) declared outside a
> function always has static storage duration, and declaring such a
> variable with the 'static' storage class makes it have internal
> linkage. So yes, an array declared outside a function is guaranteed
> have all its elements set to zero.
>


So,just to be sure, the only difference in using the actual word
'static' in the declaration/definition is the scope of that variable?

Jack Klein

8/13/2008 3:02:00 AM

0

On Tue, 12 Aug 2008 19:36:02 -0700 (PDT), mdh <mdeh@comcast.net> wrote
in comp.lang.c:

> May I ask.
>
> If an array is defined , not as a static, but outside of a function,
> is there any guarantee as to the contents of each element?
>
> Thanks.

You are a bit confused by the two different meanings that the keyword
static has had in C for a long time. The 1999 update to the standard
added a third one that is not related here.

All data objects defined at file scope, which means outside of any
function, has static storage duration and external linkage. If you
add the "static" keyword to such a definition, you change the linkage
from external to internal, which means the object cannot be referenced
by that name from another translation unit, roughly other source
files.

Any data objects defined inside a function have automatic storage
duration no linkage. The object is not initialized to any value at
all unless the definition includes an initializer, and disappears when
the function returns, and does not retain its value from one call of
the function to the next. If you add the "static" keyword to such a
definition, it changes the storage duration from automatic to static,
so the object lasts for the life of the program and keeps the last
assigned value from one function call to the next.

So to answer your question, all objects with static storage duration
and no explicit initializer in the definition are zero-initialized.
All integer types are initialized to 0, floating point types to 0.0,
and pointer types to NULL.

This applies to:

-- objects at file scope without the "static" keyword

-- objects at file scope with the "static" keyword

-- objects at function and block scope with the "static" keyword

So your array defined at file scope, outside of a function, is
guaranteed to be zero-initialized.

--
Jack Klein
Home: http://JK-Tech...
FAQs for
comp.lang.c http://...
comp.lang.c++ http://www.parashift.com/c++...
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-...

drM

8/13/2008 3:11:00 AM

0

On Aug 12, 8:01 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> On Tue, 12 Aug 2008 19:36:02 -0700 (PDT), mdh <m...@comcast.net> wrote
> in comp.lang.c:
>
>
>
> So your array defined at file scope, outside of a function, is
> guaranteed to be zero-initialized.
>
> --


Jack, thank you for that very nice explanation.

Eric Sosman

8/13/2008 3:11:00 AM

0

mdh wrote:
> May I ask.
>
> If an array is defined , not as a static, but outside of a function,
> is there any guarantee as to the contents of each element?

The keyword `static' has multiple meanings depending
on the context in which it is used.

Any array (or other variable) defined outside a function
has "static storage duration," meaning that it exists and can
hold a value throughout the entire execution of the program.
The `static' keyword in this case does not control the storage
duration, but the linkage: a `static' variable at file scope
has internal linkage ("visible within the file"), while a
variable without `static' has external linkage ("visible
throughout the program").

Inside a function, `static' has a different meaning. Any
variable not declared as `extern' has no linkage at all; it is
"visible within its block" and nowhere else. For such a variable,
`static' governs the storage duration: the variable has "static
storage duration" if declared `static', or "automatic storage
duration" otherwise.

(The latest C99 Standard introduces yet another meaning
for `static' and yet another context where it can appear, but
that need not concern us here.)

Anyhow: Any variable with static storage duration -- either
outside a function or inside a function and declared `static' --
has an initial value when the program starts. If there's an
initializer in the declaration (`int foo = 42;') the initializer
provides the value. Otherwise, the initial value is a "zero of
the right kind:" a zero value for a numeric variable, or a null
pointer for a pointer variable. An array or struct has every
element initialized to a zero of the right kind; a union's
first element is initialized similarly (remember, only one of
a union's elements holds a value at any given moment). If an
array or struct has fewer initializers than elements (as in
`int bar[42] = { 1, 2, 3 };'), the unmentioned elements are
initialized to zeroes of the right kind.

--
Eric Sosman
esosman@ieee-dot-org.invalid

s0suk3

8/13/2008 3:18:00 AM

0

On Aug 12, 9:55 pm, mdh <mdeh@comcast.net> wrote:
> On Aug 12, 7:51 pm, s0s...@gmail.com wrote:
>
> > On Aug 12, 9:36 pm, mdh <m...@comcast.net> wrote:
>
> > > If an array is defined , not as a static, but outside of a function,
> > > is there any guarantee as to the contents of each element?
>
> > A variable (of any kind, including an array) declared outside a
> > function always has static storage duration, and declaring such a
> > variable with the 'static' storage class makes it have internal
> > linkage. So yes, an array declared outside a function is guaranteed
> > have all its elements set to zero.
>
> So,just to be sure,  the only difference in using the actual word
> 'static' in the declaration/definition is the scope of that variable?


A declaration *inside* a function with the 'static' storage class
makes the variable have static storage duration (i.e., it retains it's
value from program startup until program termination).

A declaration *outside* a function with the 'static' storage class,
however, has a different meaning: it makes it have "internal
linkage" (i.e., the variable/function is visible throughout file where
it's declared, but not in other files), as opposed to "external
linkage" (i.e., the variable/function is visible to all the files in
the program), which is the default for variables/functions declared
outside any function.

The "scope" of a variable, which is what you mentioned, is something
different from "linkage." A variable can have "file scope" (i.e., it's
visible throughout the whole file where it's declared, and therefore
to all functions in the file), or "block scope" (i.e., it's declared
inside a function or inside a block inside the function, and therefore
it's visible only inside that block).

Sebastian

drM

8/13/2008 3:25:00 AM

0

On Aug 12, 8:11 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>
>      The keyword `static' has multiple meanings depending
> on the context in which it is used.
>
>      Any array (or other variable) defined outside a function
> has "static storage duration,"


snip

.
> The `static' keyword in this case .. control(s)..... the linkage:
>
>      Inside a function, `static' has a different meaning.  Any
> variable not declared as `extern' has no linkage at all; it is
> "visible within its block" and nowhere else.




Eric, I did not realize one has to explicitly declare a variable
**inside** a function with the word "extern" to confer external
linkage.
So, one can therefore use this syntax, although not sure why one
would?

extern static char a; to mean

char a has external linkage &
char a has static duration, as defined above by you.


Thank you very much.

Eric Sosman

8/13/2008 3:40:00 AM

0

mdh wrote:
>
> Eric, I did not realize one has to explicitly declare a variable
> **inside** a function with the word "extern" to confer external
> linkage.

Yes: Without `extern', a variable inside a block has
no linkage at all.

> So, one can therefore use this syntax, although not sure why one
> would?
>
> extern static char a; to mean
>
> char a has external linkage &
> char a has static duration, as defined above by you.

No: You can't have both `extern' and `static' in the
same declaration. Nor can you combine `extern' and `auto'
or `static' and `register' or `auto' and `typedef': One
specifier is the limit.

A variable declared `extern' is not a definition, but
a "link" to an actual variable defined somewhere else. More,
the "somewhere else" must have external linkage (or this
`extern' declaration couldn't "see" it) and must be outside
a function (hence, it has static storage duration and the
target of the "link" exists at all times).

--
Eric Sosman
esosman@ieee-dot-org.invalid

drM

8/13/2008 3:46:00 AM

0

On Aug 12, 8:39 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> mdh wrote:
>
> > Eric, I did not realize one has to explicitly declare a variable
> > **inside** a function with the word "extern" to confer external
> > linkage.
>
>      Yes: Without `extern', a variable inside a block has
> no linkage at all.
>
> > So, one can therefore use this syntax, although not sure why one
> > would?
>
> > extern  static char a;  to mean
>
> > char a has external linkage &
> > char a has static duration, as defined above by you.
>
>      No: You can't have both `extern' and `static' in the
> same declaration.  Nor can you combine `extern' and `auto'
> or `static' and `register' or `auto' and `typedef': One
> specifier is the limit.


ok...Eric...I think I get it...I've probably given you enough laughs
for the evening !!!! :-)


>
>      A variable declared `extern' is not a definition, but
> a "link" to an actual variable defined somewhere else.....

That was my initial understanding as well. I think I see the other
implications more clearly, so thank you for that explanation.


drM

8/13/2008 3:48:00 AM

0

On Aug 12, 8:17 pm, s0s...@gmail.com wrote:
> On Aug 12, 9:55 pm, mdh <m...@comcast.net> wrote:
>
> > On Aug 12, 7:51 pm, s0s...@gmail.com wrote:
>
> > > On Aug 12, 9:36 pm, mdh <m...@comcast.net> wrote:
>
> > > > If an array is defined , not as a static, but outside of a function,
> > > > is there any guarantee as to the contents of each element?
>
> > > A variable (of any kind, including an array) declared outside a
> > > function always has static storage duration, and declaring such a
> > > variable with the 'static' storage class makes it have internal
> > > linkage. So yes, an array declared outside a function is guaranteed
> > > have all its elements set to zero.
>
> > So,just to be sure,  the only difference in using the actual word
> > 'static' in the declaration/definition is the scope of that variable?
>
> A declaration *inside* a function with the 'static' storage class
> makes the variable have static storage duration (i.e., it retains it's
> value from program startup until program termination).
>
> A declaration *outside* a function with the 'static' storage class,
> however, has a different meaning: it makes it have "internal
> linkage" (i.e., the variable/function is visible throughout file where
> it's declared, but not in other files), as opposed to "external
> linkage" (i.e., the variable/function is visible to all the files in
> the program), which is the default for variables/functions declared
> outside any function.
>
> The "scope" of a variable, which is what you mentioned, is something
> different from "linkage." A variable can have "file scope" (i.e., it's
> visible throughout the whole file where it's declared, and therefore
> to all functions in the file), or "block scope" (i.e., it's declared
> inside a function or inside a block inside the function, and therefore
> it's visible only inside that block).
>
> Sebastian

Thank you Sebastian