Initialization of arrays

Discussion in 'C Programming' started by diegotorquemada@gmail.com, Apr 16, 2013.

  1. Guest

    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
    , Apr 16, 2013
    #1
    1. Advertising

  2. Mark Bluemel Guest

    On 16/04/2013 16:17, wrote:
    > 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.
    Mark Bluemel, Apr 16, 2013
    #2
    1. Advertising

  3. writes:

    > 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.

    [ .. ]

    --
    ... Øyvind - soon to appear in a kill file near you.
    ... Ignorance can be cured; stupidity is forever.
    Øyvind Røtvold, Apr 16, 2013
    #3
  4. Eric Sosman Guest

    On 4/16/2013 11:17 AM, wrote:
    > 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."

    --
    Eric Sosman
    d
    Eric Sosman, Apr 16, 2013
    #4
  5. Noob Guest

    Oyvind Rotvold wrote:

    > 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.
    Noob, Apr 16, 2013
    #5
  6. James Kuyper Guest

    On 04/16/2013 11:34 AM, Mark Bluemel wrote:
    > On 16/04/2013 16:17, wrote:
    >> 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.

    >> Is the GNU link wrong?


    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.
    James Kuyper, Apr 16, 2013
    #6
  7. James Kuyper Guest

    On 04/16/2013 11:36 AM, Øyvind Røtvold wrote:
    > writes:
    >
    >> 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.
    James Kuyper, Apr 16, 2013
    #7
  8. James Kuyper <> writes:

    > 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.

    --
    Lowell Gilbert, embedded/networking software engineer
    http://be-well.ilk.org/~lowell/
    Lowell Gilbert, Apr 16, 2013
    #8
  9. Guest

    On Tuesday, April 16, 2013 10:17:57 AM UTC-5, wrote:
    > 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
    , Apr 16, 2013
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    926
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    432
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    920
    Victor Bazarov
    Sep 28, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,111
    Philipp
    Jan 20, 2009
Loading...

Share This Page