can the array size be ZERO

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pranav.choudhary@gmail.com

    can the array size be ZERO

    Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
    error for the declaration

    int a[0];

    If the declaration is legal, then what will be the implications of
    saying

    a[0] = 10;

    Will it lead to memory corruption??

    Thanx,
    pranav

  • Skarmander

    #2
    Re: can the array size be ZERO

    pranav.choudhar y@gmail.com wrote:[color=blue]
    > Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
    > error for the declaration
    >
    > int a[0];
    >[/color]
    This is a gcc extension (though many other compilers support it too), so the
    answer in general is "no, this is not legal". You have to supply -pedantic
    before gcc will even think of complaining, however.
    [color=blue]
    > If the declaration is legal, then what will be the implications of
    > saying
    >
    > a[0] = 10;
    >
    > Will it lead to memory corruption??
    >[/color]
    Merely declaring an array of size 0 is undefined behavior, so the assignment
    wouldn't matter.

    If C allowed 0-sized arrays, though, then a[0] would be out of bounds, and
    the assignment produces undefined behavior. In short, anything may happen,
    including nothing, a compiler error, an OS trap, stack corruption or,
    indeed, demons flying out of your nose.

    However, the most common application of zero-sized arrays is to provide an
    alias for a pointer to an object of unspecified length, allocated by some
    unspecified means. This object may happen to be of the appropriate type, in
    which case the assignment will do what is expected.

    S.

    Comment

    • Flash Gordon

      #3
      Re: can the array size be ZERO

      pranav.choudhar y@gmail.com wrote:[color=blue]
      > Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
      > error for the declaration
      >
      > int a[0];[/color]

      It isn't legal.
      [color=blue]
      > If the declaration is legal, then what will be the implications of
      > saying
      >
      > a[0] = 10;
      >
      > Will it lead to memory corruption??[/color]

      If declaring a 0 length array way legal then trying to store something
      in the first element would invoke undefined behaviour because there
      would be no such element.
      --
      Flash Gordon, living in interesting times.
      Web site - http://home.flash-gordon.me.uk/
      comp.lang.c posting guidelines and intro:

      Comment

      • pranav.choudhary@gmail.com

        #4
        Re: can the array size be ZERO

        [color=blue]
        >
        > However, the most common application of zero-sized arrays is to provide an
        > alias for a pointer to an object of unspecified length, allocated by some
        > unspecified means. This object may happen to be of the appropriate type, in
        > which case the assignment will do what is expected.
        >
        > S.[/color]

        I did not really get the above few lines. Can you please give an
        example or a link or two for reference.

        Thanx

        Comment

        • Simon Biber

          #5
          Re: can the array size be ZERO

          pranav.choudhar y@gmail.com wrote:[color=blue][color=green]
          >>However, the most common application of zero-sized arrays is to provide an
          >>alias for a pointer to an object of unspecified length, allocated by some
          >>unspecified means. This object may happen to be of the appropriate type, in
          >>which case the assignment will do what is expected.
          >>
          >>S.[/color]
          >
          >
          > I did not really get the above few lines. Can you please give an
          > example or a link or two for reference.[/color]

          The reason many compilers supported "zero-sized arrays" is to allow this:

          struct foo
          {
          /* other members here */
          /* ... */
          int a[0]; /* nonstandard but supported by most compilers */
          };

          void do_something(si ze_t n)
          {
          struct foo *bar = malloc(n * sizeof(int));
          if(bar)
          {
          /* use bar.a[0 .. n-1] */

          free(bar);
          }
          }

          Now that in C99 we have a standard-approved way of achieving the same
          thing, it is no longer necessary to declare zero-sized arrays.

          Simon.

          Comment

          • pranav.choudhary@gmail.com

            #6
            Re: can the array size be ZERO


            [color=blue]
            > The reason many compilers supported "zero-sized arrays" is to allow this:
            >
            > struct foo
            > {
            > /* other members here */
            > /* ... */
            > int a[0]; /* nonstandard but supported by most compilers */
            > };
            >
            > void do_something(si ze_t n)
            > {
            > struct foo *bar = malloc(n * sizeof(int));
            > if(bar)
            > {
            > /* use bar.a[0 .. n-1] */
            >
            > free(bar);
            > }
            > }
            >
            > Now that in C99 we have a standard-approved way of achieving the same
            > thing, it is no longer necessary to declare zero-sized arrays.
            >
            > Simon.[/color]

            Thanks for the input...

            Comment

            • bill

              #7
              Re: can the array size be ZERO

              Simon Biber wrote:
              [color=blue]
              > The reason many compilers supported "zero-sized arrays" is to allow this:
              >
              > struct foo
              > {
              > /* other members here */
              > /* ... */
              > int a[0]; /* nonstandard but supported by most compilers */
              > };
              >
              > void do_something(si ze_t n)
              > {
              > struct foo *bar = malloc(n * sizeof(int));
              > if(bar)
              > {
              > /* use bar.a[0 .. n-1] */
              >
              > free(bar);
              > }
              > }
              >
              > Now that in C99 we have a standard-approved way of achieving the same
              > thing, it is no longer necessary to declare zero-sized arrays.
              >[/color]

              What is the standard-approved way of accomplishing that?

              Comment

              • Keith Thompson

                #8
                Re: can the array size be ZERO

                "bill" <bill.pursell@g mail.com> writes:[color=blue]
                > Simon Biber wrote:[color=green]
                >> The reason many compilers supported "zero-sized arrays" is to allow this:
                >>
                >> struct foo
                >> {
                >> /* other members here */
                >> /* ... */
                >> int a[0]; /* nonstandard but supported by most compilers */
                >> };[/color][/color]
                [snip][color=blue][color=green]
                >> Now that in C99 we have a standard-approved way of achieving the same
                >> thing, it is no longer necessary to declare zero-sized arrays.[/color]
                >
                > What is the standard-approved way of accomplishing that?[/color]

                "Flexible array members", defined in section 6.7.2.1 of the C99
                standard (google "n1124.pdf" for a copy).

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Peter Nilsson

                  #9
                  Re: can the array size be ZERO

                  pranav.choudhar y@gmail.com wrote:[color=blue]
                  > Is it legal to keep the size of an array 0.[/color]

                  That would require a conforming implementation to issue a diagnostic
                  for the
                  constraint violation. [In other words, no.]
                  [color=blue]
                  > gcc 3.4.2 did not give any error for the declaration[/color]

                  Then you did not invoke 'gcc 3.4.2' as a conforming implementation. See
                  the
                  companion online help for details of how you can invoke your compiler
                  in
                  conforming mode.
                  [color=blue]
                  > int a[0];
                  >
                  > If the declaration is legal, then what will be the implications of
                  > saying
                  >
                  > a[0] = 10;[/color]

                  If it is supported by your compiler, then you'll have to consult your
                  compiler
                  documentation on what it means.
                  [color=blue]
                  > Will it lead to memory corruption??[/color]

                  Undefined behaviour can potentially do anything.

                  --
                  Peter

                  Comment

                  Working...