Array of size zero!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashna

    #1

    Array of size zero!

    Hi all,

    I have seen a piece of code(while doing code review) which declared an
    array of size 0.One of my friend told although it is not standard
    C,some compilers will support this..I am very curious to know the use
    of it..
    The code was compiled using Diab C compiler.
    Also the array was declared in structure like this

    typedef struct someStruct
    {
    int i;
    char array[0];
    } someStruct;


    Thanks
    Regards
  • jacob navia

    #2
    Re: Array of size zero!

    The standard way of writing this is:
    typedef struct someStruct
    {
    int i;
    char array[];
    } someStruct;

    Without the zero.
    This is a flexible array and is supported by C99.

    you allocate the structure with more than sizeof(int), to
    allocate the size of the array


    Comment

    • xarax

      #3
      Re: Array of size zero!

      "prashna" <vashwath@redif fmail.com> wrote in message
      news:d40d7104.0 406160332.36604 715@posting.goo gle.com...[color=blue]
      > Hi all,
      >
      > I have seen a piece of code(while doing code review) which declared an
      > array of size 0.One of my friend told although it is not standard
      > C,some compilers will support this..I am very curious to know the use
      > of it..
      > The code was compiled using Diab C compiler.
      > Also the array was declared in structure like this
      >
      > typedef struct someStruct
      > {
      > int i;
      > char array[0];
      > } someStruct;
      >
      >
      > Thanks
      > Regards[/color]

      I think the C99 variant of this is "char array[];". The
      zero-length array thing is intended to get around the
      problem of allocating a variable-length structure, where
      the declared portion of the structure represents a fixed-length
      "header" and the size of the variable-length portion is
      calculated at run-time.

      Some compilers have a non-standard extension that allows
      an array length of zero specified, which is the same effect
      as the C99 elided length specification.

      For compilers that don't support either specification, the
      infamous "struct hack" technique is used where the array
      length is specified as 1, and size calculations for the
      variable-length structure take into consideration the
      presence of one element of the array in the structure length
      returned by sizeof().


      Comment

      • Case -

        #4
        Re: Array of size zero!

        jacob navia wrote:[color=blue]
        > The standard way of writing this is:
        > typedef struct someStruct
        > {
        > int i;
        > char array[];
        > } someStruct;
        >
        > Without the zero.
        > This is a flexible array and is supported by C99.
        >
        > you allocate the structure with more than sizeof(int), to
        > allocate the size of the array
        >[/color]

        I geuss this is not correct because it assumes that
        array[] starts right after i. As the FAQ tells us,
        you should use the offsetof() macro, which can
        calculate the byte offset of array in someStruct.
        This macro should be defined in <stddef.h>. If not,
        use the #definition in the FAQ.

        Case

        Comment

        • Richard Delorme

          #5
          Re: Array of size zero!

          prashna a écrit :[color=blue]
          > Hi all,
          >
          > I have seen a piece of code(while doing code review) which declared an
          > array of size 0.One of my friend told although it is not standard
          > C,some compilers will support this..I am very curious to know the use
          > of it..
          > The code was compiled using Diab C compiler.
          > Also the array was declared in structure like this
          >
          > typedef struct someStruct
          > {
          > int i;
          > char array[0];
          > } someStruct;[/color]

          It was a pre-C99 standard trial to have a flexible member array.
          The struct could be allocated this way :
          someStruct *s = malloc(sizeof (someStruct) + 42);
          So, with a single malloc, you allocated the struct and the array objects
          simultaneously.
          In C99 a flexible array member should be declared :
          struct someStruct {
          int i;
          char array[];
          };

          --
          Richard

          Comment

          • Dan Pop

            #6
            Re: Array of size zero!

            In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
            [color=blue]
            >jacob navia wrote:[color=green]
            >> The standard way of writing this is:
            >> typedef struct someStruct
            >> {
            >> int i;
            >> char array[];
            >> } someStruct;
            >>
            >> Without the zero.
            >> This is a flexible array and is supported by C99.
            >>
            >> you allocate the structure with more than sizeof(int), to
            >> allocate the size of the array
            >>[/color]
            >
            >I geuss this is not correct because it assumes that
            >array[] starts right after i.[/color]

            Nonsense: no such assumption is needed at all.

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • Alex Fraser

              #7
              Re: Array of size zero!

              "Dan Pop" <Dan.Pop@cern.c h> wrote in message
              news:capekg$2ju $1@sunnews.cern .ch...[color=blue]
              > In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>[/color]
              writes:[color=blue]
              >[color=green]
              > >jacob navia wrote:[color=darkred]
              > >> The standard way of writing this is:
              > >> typedef struct someStruct
              > >> {
              > >> int i;
              > >> char array[];
              > >> } someStruct;
              > >>
              > >> Without the zero.
              > >> This is a flexible array and is supported by C99.
              > >>
              > >> you allocate the structure with more than sizeof(int), to
              > >> allocate the size of the array
              > >>[/color]
              > >
              > >I geuss this is not correct because it assumes that
              > >array[] starts right after i.[/color]
              >
              > Nonsense: no such assumption is needed at all.[/color]

              Right, but Jacob /is/ making that assumption. AIUI, this does not
              necessarily allocate sufficient space for 10 elements in array:

              struct someStruct *s = malloc(sizeof(i nt) + 10);

              But, OTOH, this does:

              struct someStruct *s = malloc(sizeof *s + 10);

              (Assuming malloc() succeeds in both cases.)

              The reason being that padding after i in struct someStruct is not allowed
              for.

              Alex


              Comment

              • Dan Pop

                #8
                Re: Array of size zero!

                In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
                [color=blue]
                >"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                >news:capekg$2j u$1@sunnews.cer n.ch...[color=green]
                >> In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>[/color]
                >writes:[color=green]
                >>[color=darkred]
                >> >jacob navia wrote:
                >> >> The standard way of writing this is:
                >> >> typedef struct someStruct
                >> >> {
                >> >> int i;
                >> >> char array[];
                >> >> } someStruct;
                >> >>
                >> >> Without the zero.
                >> >> This is a flexible array and is supported by C99.
                >> >>
                >> >> you allocate the structure with more than sizeof(int), to
                >> >> allocate the size of the array
                >> >>
                >> >
                >> >I geuss this is not correct because it assumes that
                >> >array[] starts right after i.[/color]
                >>
                >> Nonsense: no such assumption is needed at all.[/color]
                >
                >Right, but Jacob /is/ making that assumption.[/color]

                Where?!? He says more than sizeof(int), which might be misleading, but
                not technically wrong.
                [color=blue]
                >AIUI, this does not
                >necessarily allocate sufficient space for 10 elements in array:
                >
                >struct someStruct *s = malloc(sizeof(i nt) + 10);[/color]

                It should, except for deliberately perverse implementations , but, in
                theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                the right way of allocating memory for the structure, did he?
                [color=blue]
                >But, OTOH, this does:
                >
                >struct someStruct *s = malloc(sizeof *s + 10);
                >
                >(Assuming malloc() succeeds in both cases.)
                >
                >The reason being that padding after i in struct someStruct is not allowed
                >for. ^^^[/color]

                The reason being that padding after i in struct someStruct is
                theoretically allowed. But, since it would merely waste memory, don't
                expect to find it, until someone revives the DS9K project.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Emmanuel Delahaye

                  #9
                  Re: Array of size zero!

                  In 'comp.lang.c', vashwath@rediff mail.com (prashna) wrote:
                  [color=blue]
                  > I have seen a piece of code(while doing code review) which declared an
                  > array of size 0.One of my friend told although it is not standard[/color]

                  Wrong. It's a C99 feature.
                  [color=blue]
                  > C,some compilers will support this..I am very curious to know the use
                  > of it..
                  > The code was compiled using Diab C compiler.
                  > Also the array was declared in structure like this
                  >
                  > typedef struct someStruct
                  > {
                  > int i;
                  > char array[0];
                  > } someStruct;[/color]

                  It's used to do some linear mapping. It's a sort of trick... Not very good C
                  (outbounds-based technique), but it works and can be useful.

                  --
                  -ed- get my email here: http://marreduspam.com/ad672570
                  The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                  C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
                  FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

                  Comment

                  • Alex Fraser

                    #10
                    Re: Array of size zero!

                    "Dan Pop" <Dan.Pop@cern.c h> wrote in message
                    news:capoii$n3g $2@sunnews.cern .ch...[color=blue]
                    > In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:[color=green]
                    > >"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                    > >news:capekg$2j u$1@sunnews.cer n.ch...[color=darkred]
                    > >> In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case -
                    > >> <no@no.no> writes:
                    > >>
                    > >> >jacob navia wrote:
                    > >> >> The standard way of writing this is:
                    > >> >> typedef struct someStruct
                    > >> >> {
                    > >> >> int i;
                    > >> >> char array[];
                    > >> >> } someStruct;[/color][/color][/color]
                    [snip][color=blue][color=green][color=darkred]
                    > >> >> you allocate the structure with more than sizeof(int), to
                    > >> >> allocate the size of the array
                    > >> >
                    > >> >I geuss this is not correct because it assumes that
                    > >> >array[] starts right after i.
                    > >>
                    > >> Nonsense: no such assumption is needed at all.[/color]
                    > >
                    > >Right, but Jacob /is/ making that assumption.[/color]
                    >
                    > Where?!? He says more than sizeof(int), which might be misleading, but
                    > not technically wrong.[/color]

                    By fortunate vagueness, yes. That too is an assumption, but a reasonable
                    one, don't you think?
                    [color=blue][color=green]
                    > >AIUI, this does not
                    > >necessarily allocate sufficient space for 10 elements in array:
                    > >
                    > >struct someStruct *s = malloc(sizeof(i nt) + 10);[/color]
                    >
                    > It should, except for deliberately perverse implementations , but, in
                    > theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                    > the right way of allocating memory for the structure, did he?[/color]

                    See above.
                    [color=blue][color=green]
                    > >But, OTOH, this does:
                    > >
                    > >struct someStruct *s = malloc(sizeof *s + 10);
                    > >
                    > >(Assuming malloc() succeeds in both cases.)
                    > >
                    > >The reason being that padding after i in struct someStruct is not allowed
                    > >for. ^^^[/color][/color]

                    (I wrote this as applying to the first sentence but it doesn't read that
                    way. Oops.)
                    [color=blue]
                    > The reason being that padding after i in struct someStruct is
                    > theoretically allowed. But, since it would merely waste memory, don't
                    > expect to find it, until someone revives the DS9K project.[/color]

                    Agreed, in this specific case it is extremely unlikely. But for some
                    combinations of types of i and/or array, it's almost "guaranteed " (eg struct
                    {int i, double array[]}).

                    Alex


                    Comment

                    • Case -

                      #11
                      Re: Array of size zero!

                      Dan Pop wrote:
                      [color=blue]
                      > In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
                      >
                      >[color=green]
                      >>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                      >>news:capekg$2 ju$1@sunnews.ce rn.ch...
                      >>[color=darkred]
                      >>>In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>[/color]
                      >>
                      >>writes:
                      >>[color=darkred]
                      >>>>jacob navia wrote:
                      >>>>
                      >>>>>The standard way of writing this is:
                      >>>>>typedef struct someStruct
                      >>>>>{
                      >>>>> int i;
                      >>>>> char array[];
                      >>>>>} someStruct;
                      >>>>>
                      >>>>>Without the zero.
                      >>>>>This is a flexible array and is supported by C99.
                      >>>>>
                      >>>>>you allocate the structure with more than sizeof(int), to
                      >>>>>allocate the size of the array
                      >>>>>
                      >>>>
                      >>>>I geuss this is not correct because it assumes that
                      >>>>array[] starts right after i.
                      >>>
                      >>>Nonsense: no such assumption is needed at all.[/color]
                      >>
                      >>Right, but Jacob /is/ making that assumption.[/color]
                      >
                      >
                      > Where?!? He says more than sizeof(int), which might be misleading, but
                      > not technically wrong.
                      >
                      >[color=green]
                      >>AIUI, this does not
                      >>necessarily allocate sufficient space for 10 elements in array:
                      >>
                      >>struct someStruct *s = malloc(sizeof(i nt) + 10);[/color]
                      >
                      >
                      > It should, except for deliberately perverse implementations , but, in
                      > theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                      > the right way of allocating memory for the structure, did he?[/color]

                      This is the theoretical case I was addressing. So, no nonsense
                      according to the C spec! And, come on, what else could Jacob
                      have meant?

                      Case


                      Comment

                      • Keith Thompson

                        #12
                        Re: Array of size zero!

                        Emmanuel Delahaye <emdelYOURBRA@n oos.fr> writes:[color=blue]
                        > In 'comp.lang.c', vashwath@rediff mail.com (prashna) wrote:
                        >[color=green]
                        > > I have seen a piece of code(while doing code review) which declared an
                        > > array of size 0.One of my friend told although it is not standard[/color]
                        >
                        > Wrong. It's a C99 feature.[/color]

                        No, C99 doesn't allow zero-sized arrays. There is a new feature
                        called "flexible array members" (discussed elsewhere in this thread),
                        but they're not implemented with zero-sized arrays.

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

                        • Dan Pop

                          #13
                          Re: Array of size zero!

                          In <40d0cbd1$0$842 19$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
                          [color=blue]
                          >Dan Pop wrote:
                          >[color=green]
                          >> In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
                          >>
                          >>[color=darkred]
                          >>>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                          >>>news:capekg$ 2ju$1@sunnews.c ern.ch...
                          >>>
                          >>>>In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>
                          >>>
                          >>>writes:
                          >>>
                          >>>>>jacob navia wrote:
                          >>>>>
                          >>>>>>The standard way of writing this is:
                          >>>>>>typedef struct someStruct
                          >>>>>>{
                          >>>>>> int i;
                          >>>>>> char array[];
                          >>>>>>} someStruct;
                          >>>>>>
                          >>>>>>Without the zero.
                          >>>>>>This is a flexible array and is supported by C99.
                          >>>>>>
                          >>>>>>you allocate the structure with more than sizeof(int), to
                          >>>>>>allocat e the size of the array
                          >>>>>>
                          >>>>>
                          >>>>>I geuss this is not correct because it assumes that
                          >>>>>array[] starts right after i.
                          >>>>
                          >>>>Nonsense: no such assumption is needed at all.
                          >>>
                          >>>Right, but Jacob /is/ making that assumption.[/color]
                          >>
                          >>
                          >> Where?!? He says more than sizeof(int), which might be misleading, but
                          >> not technically wrong.
                          >>
                          >>[color=darkred]
                          >>>AIUI, this does not
                          >>>necessaril y allocate sufficient space for 10 elements in array:
                          >>>
                          >>>struct someStruct *s = malloc(sizeof(i nt) + 10);[/color]
                          >>
                          >>
                          >> It should, except for deliberately perverse implementations , but, in
                          >> theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                          >> the right way of allocating memory for the structure, did he?[/color]
                          >
                          >This is the theoretical case I was addressing. So, no nonsense
                          >according to the C spec! And, come on, what else could Jacob
                          >have meant?[/color]

                          Ask him. The point is that offsetof is NEVER necessary in this case and
                          Jacob said "more than sizeof(int)", which is technically correct.

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de

                          Comment

                          • Case -

                            #14
                            Re: Array of size zero!

                            Dan Pop wrote:
                            [color=blue]
                            > In <40d0cbd1$0$842 19$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
                            >
                            >[color=green]
                            >>Dan Pop wrote:
                            >>
                            >>[color=darkred]
                            >>>In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
                            >>>
                            >>>
                            >>>
                            >>>>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                            >>>>news:capekg $2ju$1@sunnews. cern.ch...
                            >>>>
                            >>>>
                            >>>>>In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>
                            >>>>
                            >>>>writes:
                            >>>>
                            >>>>
                            >>>>>>jacob navia wrote:
                            >>>>>>
                            >>>>>>
                            >>>>>>>The standard way of writing this is:
                            >>>>>>>typede f struct someStruct
                            >>>>>>>{
                            >>>>>>> int i;
                            >>>>>>> char array[];
                            >>>>>>>} someStruct;
                            >>>>>>>
                            >>>>>>>Withou t the zero.
                            >>>>>>>This is a flexible array and is supported by C99.
                            >>>>>>>
                            >>>>>>>you allocate the structure with more than sizeof(int), to
                            >>>>>>>alloca te the size of the array
                            >>>>>>>
                            >>>>>>
                            >>>>>>I geuss this is not correct because it assumes that
                            >>>>>>array[] starts right after i.
                            >>>>>
                            >>>>>Nonsense : no such assumption is needed at all.
                            >>>>
                            >>>>Right, but Jacob /is/ making that assumption.
                            >>>
                            >>>
                            >>>Where?!? He says more than sizeof(int), which might be misleading, but
                            >>>not technically wrong.
                            >>>
                            >>>
                            >>>
                            >>>>AIUI, this does not
                            >>>>necessari ly allocate sufficient space for 10 elements in array:
                            >>>>
                            >>>>struct someStruct *s = malloc(sizeof(i nt) + 10);
                            >>>
                            >>>
                            >>>It should, except for deliberately perverse implementations , but, in
                            >>>theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                            >>>the right way of allocating memory for the structure, did he?[/color]
                            >>
                            >>This is the theoretical case I was addressing. So, no nonsense
                            >>according to the C spec! And, come on, what else could Jacob
                            >>have meant?[/color]
                            >
                            >
                            > Ask him. The point is that offsetof is NEVER necessary in this case and
                            > Jacob said "more than sizeof(int)", which is technically correct.[/color]

                            With 'NEVER' do you mean with ALL real-life implementations , or do
                            you mean according to the C standard?

                            He said "more than sizeof(int), to allocate the size of the array".
                            IAW: 'Everything more than sizeof(int), is the size of the array'.
                            When more == 1, and there's a padding of >= 1 bytes between the int
                            and the array ... So, not technically correct.

                            Case

                            Comment

                            • Dan Pop

                              #15
                              Re: Array of size zero!

                              In <40d1a865$0$842 16$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
                              [color=blue]
                              >Dan Pop wrote:
                              >[color=green]
                              >> In <40d0cbd1$0$842 19$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
                              >>
                              >>[color=darkred]
                              >>>Dan Pop wrote:
                              >>>
                              >>>
                              >>>>In <2jb2vaFv5itsU1 @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
                              >>>>
                              >>>>
                              >>>>
                              >>>>>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                              >>>>>news:capek g$2ju$1@sunnews .cern.ch...
                              >>>>>
                              >>>>>
                              >>>>>>In <40d0330f$0$417 49$5fc3050@drea der2.news.tisca li.nl> Case - <no@no.no>
                              >>>>>
                              >>>>>writes:
                              >>>>>
                              >>>>>
                              >>>>>>>jacob navia wrote:
                              >>>>>>>
                              >>>>>>>
                              >>>>>>>>The standard way of writing this is:
                              >>>>>>>>typed ef struct someStruct
                              >>>>>>>>{
                              >>>>>>>> int i;
                              >>>>>>>> char array[];
                              >>>>>>>>} someStruct;
                              >>>>>>>>
                              >>>>>>>>Witho ut the zero.
                              >>>>>>>>This is a flexible array and is supported by C99.
                              >>>>>>>>
                              >>>>>>>>you allocate the structure with more than sizeof(int), to
                              >>>>>>>>allocat e the size of the array
                              >>>>>>>>
                              >>>>>>>
                              >>>>>>>I geuss this is not correct because it assumes that
                              >>>>>>>array[] starts right after i.
                              >>>>>>
                              >>>>>>Nonsens e: no such assumption is needed at all.
                              >>>>>
                              >>>>>Right, but Jacob /is/ making that assumption.
                              >>>>
                              >>>>
                              >>>>Where?!? He says more than sizeof(int), which might be misleading, but
                              >>>>not technically wrong.
                              >>>>
                              >>>>
                              >>>>
                              >>>>>AIUI, this does not
                              >>>>>necessaril y allocate sufficient space for 10 elements in array:
                              >>>>>
                              >>>>>struct someStruct *s = malloc(sizeof(i nt) + 10);
                              >>>>
                              >>>>
                              >>>>It should, except for deliberately perverse implementations , but, in
                              >>>>theory, you're correct. OTOH, Jacob didn't say anywhere that this is
                              >>>>the right way of allocating memory for the structure, did he?
                              >>>
                              >>>This is the theoretical case I was addressing. So, no nonsense
                              >>>according to the C spec! And, come on, what else could Jacob
                              >>>have meant?[/color]
                              >>
                              >>
                              >> Ask him. The point is that offsetof is NEVER necessary in this case and
                              >> Jacob said "more than sizeof(int)", which is technically correct.[/color]
                              >
                              >With 'NEVER' do you mean with ALL real-life implementations , or do
                              >you mean according to the C standard?[/color]

                              Both. All you need is sizeof(somestru ct) to which you add the number of
                              bytes you need for array.
                              [color=blue]
                              >He said "more than sizeof(int), to allocate the size of the array".
                              >IAW: 'Everything more than sizeof(int), is the size of the array'.[/color]

                              Severely broken interpretation. The right amount is simply unspecified
                              in the original statement. E.g. "you need more than your $10 to buy this
                              car" doesn't imply that you can buy it for $10.01.
                              [color=blue]
                              >When more == 1, and there's a padding of >= 1 bytes between the int
                              >and the array ... So, not technically correct.[/color]

                              Nope.

                              Dan
                              --
                              Dan Pop
                              DESY Zeuthen, RZ group
                              Email: Dan.Pop@ifh.de

                              Comment

                              Working...