Initialization of arrays

D

diegotorquemada

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

In that sense my_array[4] is indeterminate (provided it is not static)

Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that

int my_array[5] = { 0 };

will initialize all the array to zero?

Is the GNU link wrong?

Thank you very much and kind regards,

Diego Andrés
 
M

Mark Bluemel

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

In that sense my_array[4] is indeterminate (provided it is not static)

Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that

int my_array[5] = { 0 };

will initialize all the array to zero?

Is the GNU link wrong?

The footnote to the FAQ page you quoted suggests that it is.
 
Ø

Øyvind Røtvold

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

In that sense my_array[4] is indeterminate (provided it is not static)

Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that

int my_array[5] = { 0 };

will initialize all the array to zero?

The link (http://c-faq.com/decl/initval.html) says that items that are
not explicitly initialized will have an indeterminate value unless the
array is of static storage duration, which is exactly the same thing
that you said, so no contradiction there.

[ .. ]
 
E

Eric Sosman

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

In that sense my_array[4] is indeterminate (provided it is not static)

Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that

int my_array[5] = { 0 };

will initialize all the array to zero?

Is the GNU link wrong?

Yes. 6.7.9p21: "If there are fewer initializers in a
brace-enclosed list than there are elements or members of
an aggregate, [...] the remainder of the aggregate shall be
initialized implicitly the same as objects that have static
storage duration."
 
N

Noob

Oyvind said:
The link (http://c-faq.com/decl/initval.html) says that items that
are not explicitly initialized will have an indeterminate value
unless the array is of static storage duration, which is exactly the
same thing that you said, so no contradiction there.

The problem is on the other page:

https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example,
this code initializes only the first three elements:

int my_array[5] = { 0, 1, 2 };

Items that are not explicitly initialized will have an indeterminate
value unless the array is of static storage duration. For arrays of
static storage duration, arithmetic types are initialized as zero and
pointers are initialized as NULL.

So, in the example above, if the definition is at file level or is
preceded by static, the final two elements will be 0. Otherwise, the
final two elements will have an indeterminate value.

The last paragraph is wrong. Even if my_array has automatic storage
duration, my_array[3] and my_array[4] are guaranteed to be 0.
The final two elements will NOT have "indeterminate value".

Regards.
 
J

James Kuyper

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

The C standard requires that, if any element of an array is explicitly
initialized, all elements that are not explicitly initialized must be
implicitly zero-initialized. There are no exceptions to this rule based
upon the storage duration. Automatic array elements only have
indeterminate values if no initializers are provided.

Yes. I just wrote a test program, and elements of an partially
-initialized array that were not explicitly set to a different value had
a value of zero, as they should have. However, an indeterminate value
could happen to be zero, so that doesn't prove that the page is
incorrect as a description of the way gcc actually works - but it's
strong evidence against that being the case. The page is unambiguously
incorrect as a description of what the C standard requires of conforming
implementations.
The footnote to the FAQ page you quoted suggests that it is.

There are three footnotes on that page. The third footnote describes
"Early printings of K&R2" as being incorrect when they "stated that
partially-initialized automatic aggregates were filled out with garbage.".

The gnu page doesn't claim to describe the language described by "early
printings of K&R2". It claims to use C89 as the basis for the
description, with C99 changes and GnuC extensions to C89 labelled as
such. That item is not labelled as a GnuC extension, and this is not a
feature that changed in C99, so it is simply wrong.
 
J

James Kuyper

Hello all,

According to:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays
You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.

In that sense my_array[4] is indeterminate (provided it is not static)

Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that

int my_array[5] = { 0 };

will initialize all the array to zero?

The link (http://c-faq.com/decl/initval.html) says that items that are
not explicitly initialized will have an indeterminate value unless the
array is of static storage duration, which is exactly the same thing
that you said, so no contradiction there.

No, the relevant part of the FAQ page says "When an automatic array or
structure has a partial initializer, the remainder is initialized to 0,
just as for statics.", which is consistent with the requirements of the
C standard, and inconsistent with what it says on the gnu page.
 
L

Lowell Gilbert

James Kuyper said:
The gnu page doesn't claim to describe the language described by "early
printings of K&R2". It claims to use C89 as the basis for the
description, with C99 changes and GnuC extensions to C89 labelled as
such. That item is not labelled as a GnuC extension, and this is not a
feature that changed in C99, so it is simply wrong.

In fact, gcc's manual's examples of their various extensions to array
handling are consistent and specific that unmentioned parts of the array
are filled as if they had been explicitly initialized to zero.
 
D

diegotorquemada

Hello all,



According to:

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Initializing-Arrays


You don't have to initialize all of the array elements. For example, this code initializes only the first three elements:
int my_array[5] = { 0, 1, 2 };
Items that are not explicitly initialized will have an indeterminate value unless the array is of static storage duration.



In that sense my_array[4] is indeterminate (provided it is not static)



Then why often you see in some other places (see e.g. http://c-faq.com/decl/initval.html) that



int my_array[5] = { 0 };



will initialize all the array to zero?



Is the GNU link wrong?



Thank you very much and kind regards,



Diego Andrés

Hi all, Thank you very much for your comments... I have just informed the bug in the GNU tutorial.

Kind regards,

Diego
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top