Array definition and extern declaration with different size.

Discussion in 'C Programming' started by joshc, Jul 31, 2006.

  1. joshc

    joshc Guest

    Hi,

    I have an array defined in one file with an intializer as follows:

    int arr[] = {0, 1, 2, 3};

    I have a declaration of the array in another file as follows:

    extern int arr[10];

    This compiles without a problem on my implementation and arr ends up
    being of size 10 on my implementation. Is this legal in "standard C"? I
    was reading question 1.24 of the FAQ but that didn't seem to answer my
    question in this case. Is the array definition with the initializer
    somehow an incomplete definition?

    Thanks for the help,

    Josh
    joshc, Jul 31, 2006
    #1
    1. Advertising

  2. joshc

    Endy. Guest

    I think it compiles OK, but may cause segmentation fault when run.
    "joshc" <>
    ??????:...
    > Hi,
    >
    > I have an array defined in one file with an intializer as follows:
    >
    > int arr[] = {0, 1, 2, 3};
    >
    > I have a declaration of the array in another file as follows:
    >
    > extern int arr[10];
    >
    > This compiles without a problem on my implementation and arr ends up
    > being of size 10 on my implementation. Is this legal in "standard C"? I
    > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > question in this case. Is the array definition with the initializer
    > somehow an incomplete definition?
    >
    > Thanks for the help,
    >
    > Josh
    >
    Endy., Jul 31, 2006
    #2
    1. Advertising

  3. joshc

    Jack Klein Guest

    On 30 Jul 2006 20:48:10 -0700, "joshc" <> wrote in
    comp.lang.c:

    > Hi,
    >
    > I have an array defined in one file with an intializer as follows:
    >
    > int arr[] = {0, 1, 2, 3};
    >
    > I have a declaration of the array in another file as follows:
    >
    > extern int arr[10];
    >
    > This compiles without a problem on my implementation and arr ends up
    > being of size 10 on my implementation. Is this legal in "standard C"? I
    > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > question in this case. Is the array definition with the initializer
    > somehow an incomplete definition?
    >
    > Thanks for the help,
    >
    > Josh


    Any size specified in the external declaration is ignored by the
    compiler. The array contains four ints. Any attempt to access past
    arr [3] produces undefined behavior.

    This is the same as using a size in an array style parameter
    definition (an unfortunate choice anyway), with the exception of the
    C99 feature where the "static" keyword is used.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 31, 2006
    #3
  4. Jack Klein wrote:
    > On 30 Jul 2006 20:48:10 -0700, "joshc" <> wrote in
    > comp.lang.c:
    >
    > > Hi,
    > >
    > > I have an array defined in one file with an intializer as follows:
    > >
    > > int arr[] = {0, 1, 2, 3};
    > >
    > > I have a declaration of the array in another file as follows:
    > >
    > > extern int arr[10];
    > >
    > > This compiles without a problem on my implementation and arr ends up
    > > being of size 10 on my implementation. Is this legal in "standard C"? I
    > > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > > question in this case. Is the array definition with the initializer
    > > somehow an incomplete definition?
    > >
    > > Thanks for the help,
    > >
    > > Josh

    >
    > Any size specified in the external declaration is ignored by the
    > compiler. The array contains four ints. Any attempt to access past
    > arr [3] produces undefined behavior.
    >
    > This is the same as using a size in an array style parameter
    > definition (an unfortunate choice anyway), with the exception of the
    > C99 feature where the "static" keyword is used.


    That may be what happens for some compilers, but it is not what the
    standard says, and if I recall correctly, there are implementations
    that would display a warning or error message for such code during
    linking.

    n1124 6.2.7#2:
    "All declarations that refer to the same object or function shall have
    compatible type; otherwise, the behavior is undeï¬ned."

    n1124 6.7.5.2#6:
    "For two array types to be compatible, both shall have compatible
    element types, and if both size speciï¬ers are present, and are
    integer constant expressions, then both size speciï¬ers shall have the
    same constant value. If the two array types are used in a context which
    requires them to be compatible, it is undeï¬ned behavior if the two
    size speciï¬ers evaluate to unequal values."
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jul 31, 2006
    #4
  5. joshc

    Guest

    Harald van Dijk wrote:

    > n1124 6.2.7#2:
    > "All declarations that refer to the same object or function shall have
    > compatible type; otherwise, the behavior is undeï¬ned."
    >
    > n1124 6.7.5.2#6:
    > "For two array types to be compatible, both shall have compatible
    > element types, and if both size speciï¬ers are present, and are
    > integer constant expressions, then both size speciï¬ers shall have the
    > same constant value. If the two array types are used in a context which
    > requires them to be compatible, it is undeï¬ned behavior if the two
    > size speciï¬ers evaluate to unequal values."


    What does this tell us for the case where one of the size specifiers
    is not present ?
    , Jul 31, 2006
    #5
  6. Harald van Dijk wrote:
    > Jack Klein wrote:
    > > On 30 Jul 2006 20:48:10 -0700, "joshc" <> wrote in
    > > comp.lang.c:
    > >
    > > > Hi,
    > > >
    > > > I have an array defined in one file with an intializer as follows:
    > > >
    > > > int arr[] = {0, 1, 2, 3};
    > > >
    > > > I have a declaration of the array in another file as follows:
    > > >
    > > > extern int arr[10];
    > > >
    > > > This compiles without a problem on my implementation and arr ends up
    > > > being of size 10 on my implementation. Is this legal in "standard C"? I
    > > > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > > > question in this case. Is the array definition with the initializer
    > > > somehow an incomplete definition?
    > > >
    > > > Thanks for the help,
    > > >
    > > > Josh

    > >
    > > Any size specified in the external declaration is ignored by the
    > > compiler. The array contains four ints. Any attempt to access past
    > > arr [3] produces undefined behavior.
    > >
    > > This is the same as using a size in an array style parameter
    > > definition (an unfortunate choice anyway), with the exception of the
    > > C99 feature where the "static" keyword is used.

    >
    > That may be what happens for some compilers, but it is not what the
    > standard says, and if I recall correctly, there are implementations
    > that would display a warning or error message for such code during
    > linking.
    >
    > n1124 6.2.7#2:
    > "All declarations that refer to the same object or function shall have
    > compatible type; otherwise, the behavior is undeï¬ned."
    >
    > n1124 6.7.5.2#6:
    > "For two array types to be compatible, both shall have compatible
    > element types, and if both size speciï¬ers are present, and are
    > integer constant expressions, then both size speciï¬ers shall have the
    > same constant value. If the two array types are used in a context which
    > requires them to be compatible, it is undeï¬ned behavior if the two
    > size speciï¬ers evaluate to unequal values."


    Hmm. Re-reading it, though, I think you may technically be right for
    this particular case, but not for "int arr[4] = {0, 1, 2, 3};"

    Both 'int arr[]' and 'extern int arr[10]' have compatible (identical,
    even) element type, and even though "int arr[] = {0, 1, 2, 3};"
    declares arr as int [4] (per 6.7.8#22), it is not defined with a size
    specifier (a syntactic construct), so it seems the behaviour is not
    actually undefined. Maybe comp.std.c can help out.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jul 31, 2006
    #6
  7. wrote:
    > Harald van Dijk wrote:
    >
    > > n1124 6.2.7#2:
    > > "All declarations that refer to the same object or function shall have
    > > compatible type; otherwise, the behavior is undeï¬ned."
    > >
    > > n1124 6.7.5.2#6:
    > > "For two array types to be compatible, both shall have compatible
    > > element types, and if both size speciï¬ers are present, and are
    > > integer constant expressions, then both size speciï¬ers shall have the
    > > same constant value. If the two array types are used in a context which
    > > requires them to be compatible, it is undeï¬ned behavior if the two
    > > size speciï¬ers evaluate to unequal values."

    >
    > What does this tell us for the case where one of the size specifiers
    > is not present ?


    Yeah, I noticed that too after posting. "int arr[] = { 0, 1, 2, 3 };"
    defines arr as int[4], and (&arr) + 1 is allowed, though, so it's meant
    to be undefined, I think, but it actually isn't in this case.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jul 31, 2006
    #7
  8. joshc

    joshc Guest

    Jack Klein wrote:
    > On 30 Jul 2006 20:48:10 -0700, "joshc" <> wrote in
    > comp.lang.c:
    >
    > > Hi,
    > >
    > > I have an array defined in one file with an intializer as follows:
    > >
    > > int arr[] = {0, 1, 2, 3};
    > >
    > > I have a declaration of the array in another file as follows:
    > >
    > > extern int arr[10];
    > >
    > > This compiles without a problem on my implementation and arr ends up
    > > being of size 10 on my implementation. Is this legal in "standard C"? I
    > > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > > question in this case. Is the array definition with the initializer
    > > somehow an incomplete definition?
    > >
    > > Thanks for the help,
    > >
    > > Josh

    >
    > Any size specified in the external declaration is ignored by the
    > compiler. The array contains four ints. Any attempt to access past
    > arr [3] produces undefined behavior.
    >
    > This is the same as using a size in an array style parameter
    > definition (an unfortunate choice anyway), with the exception of the
    > C99 feature where the "static" keyword is used.
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://c-faq.com/
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


    Thanks, Jack. I figured it was probably just how my compiler behaved
    but I was surprised that the space allocated for the array was based on
    the external declaration. I was expecting a warning but I guess that's
    not required.

    Thanks again.
    joshc, Jul 31, 2006
    #8
  9. On Mon, 31 Jul 2006 03:48:10 UTC, "joshc" <>
    wrote:

    > Hi,
    >
    > I have an array defined in one file with an intializer as follows:
    >
    > int arr[] = {0, 1, 2, 3};


    This defines an array of 4 members. When the number of members defined
    through the initialisers is too low you must define the real size to
    get the list of initialisers expanded to the real number of members
    you needs.

    > I have a declaration of the array in another file as follows:
    >
    > extern int arr[10];


    The best way would be:

    insert the declaration into an header file and include it even on the
    file you has to write the definition.

    This will give the compiler the chance to extend the definition [] to
    the size the declaration defines. On the other hand you can simply use
    'extern int arr[]' in other translation units.
    So 'int arr[ARR_SIZE] = { [incomplete] list of initialisers }; and
    'extern arr[ARR_SIZE]' in a common header while ARR_SIZE describes the
    size you needs really will give you in any place the chance to change
    the arraysize only on one place.

    > This compiles without a problem on my implementation and arr ends up
    > being of size 10 on my implementation. Is this legal in "standard C"? I
    > was reading question 1.24 of the FAQ but that didn't seem to answer my
    > question in this case. Is the array definition with the initializer
    > somehow an incomplete definition?
    >
    > Thanks for the help,
    >
    > Josh
    >



    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
    Herbert Rosenau, Jul 31, 2006
    #9
  10. joshc

    Guest

    wrote:
    >
    > Harald van D??k wrote:
    >>
    >> n1124 6.7.5.2#6:
    >> "For two array types to be compatible, both shall have compatible
    >> element types, and if both size specifiers are present, and are
    >> integer constant expressions, then both size specifiers shall have the
    >> same constant value. If the two array types are used in a context which
    >> requires them to be compatible, it is undefined behavior if the two
    >> size specifiers evaluate to unequal values."

    >
    > What does this tell us for the case where one of the size specifiers
    > is not present ?


    It tells you that the committee isn't infallible (in case you hadn't
    already discovered that). I'm sure the intent is that a size imputed
    from an initialization behave just like a constant size, but the
    document doesn't actually say so.

    -Larry Jones

    Don't you hate it when your boogers freeze? -- Calvin
    , Aug 1, 2006
    #10
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,355
    tom_usenet
    Aug 2, 2004
  2. Replies:
    12
    Views:
    911
    Dan Pop
    Aug 23, 2004
  3. Replies:
    11
    Views:
    489
    Al Balmer
    Mar 28, 2007
  4. Andre
    Replies:
    5
    Views:
    512
    Keith Thompson
    Jul 17, 2012
  5. Noob

    Declaration vs definition of array

    Noob, Mar 27, 2013, in forum: C Programming
    Replies:
    18
    Views:
    365
    Tim Rentsch
    Apr 2, 2013
Loading...

Share This Page