Nested flexible array members 
Author Message
 Nested flexible array members

Can the base type of a flexible array member be a structure that
contains a flexible array member?  For example, would the following be
legal?

struct S{
     int i;
     char c[];

Quote:
};

struct T{
     int j;
     struct S s[];
Quote:
};

--



Mon, 05 May 2003 03:00:00 GMT  
 Nested flexible array members
This could be way off base, but aren't you really just declaring pointers
with the empty array brackets?

If that's the case, then this should work find, as long as the structure you
are declaring a pointer to is at least declared (not necessarily defined)
prior to the definition of the structure that contains the pointer.

Quote:

> Can the base type of a flexible array member be a structure that
> contains a flexible array member?  For example, would the following be
> legal?

> struct S{
>      int i;
>      char c[];
> };

> struct T{
>      int j;
>      struct S s[];
> };
> --


--



Tue, 06 May 2003 03:00:00 GMT  
 Nested flexible array members

Quote:

> Can the base type of a flexible array member be a structure that
> contains a flexible array member?  For example, would the following be
> legal?

> struct S{
>      int i;
>      char c[];
> };

> struct T{
>      int j;
>      struct S s[];
> };
> --


Hmm- I know I am on _very_ thin ice here, and it's a question, not
an attempt to answer:

I can't imagine how that could work.
Struct S may be expanded at it's end with some chars.
struct T may be expanded with some struct's S at it's end, where
every struct S could be of different size.
I have no idea how an implementation could manage that.

TIA for any clarification
Robert

--

I don't make my mistakes more than once. I store them carefully and
after some time I take them out again, add some new features and
_reuse_ them.
--



Tue, 06 May 2003 03:00:00 GMT  
 Nested flexible array members

Quote:

> Can the base type of a flexible array member be a structure that
> contains a flexible array member?

No.  C99 section 6.7.2.1:

        ...the last member of a structure with more than one
        named member may have incomplete array type; such a
        structure... shall not be a member of a structure or an
        element of an array.
--



Tue, 06 May 2003 03:00:00 GMT  
 Nested flexible array members

Quote:

> Can the base type of a flexible array member be a structure that
> contains a flexible array member?  For example, would the following be
> legal?

> struct S{
>      int i;
>      char c[];
> };

> struct T{
>      int j;
>      struct S s[];
> };

Since an array consists of objects with a particular member object type,
and a stucture containing a flexible array is an incomplete type and
cannot be completed, and incomplete types are not object types, I'd say
no.

Richard
--



Tue, 06 May 2003 03:00:00 GMT  
 Nested flexible array members
# Can the base type of a flexible array member be a structure that
# contains a flexible array member?  For example, would the following be
# legal?

# struct S{
#      int i;
#      char c[];
# };

# struct T{
#      int j;
#      struct S s[];
# };

My reading of ISO 9899:1999 is "no". Because 6.7.2.1 #16 says that "As a
special case, the last element of a structure with more than one named
member may have an incomplete array type; this is called a flexible
array member". And 6.7.5.2. #1 Array declarators: "The element type
shall not be an incomplete ... type". If my reading is correct (and the
incompleteness of the array c extends to the struct S), a translator has
to diagnose your struct T declaration.

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Tue, 06 May 2003 03:00:00 GMT  
 Nested flexible array members

Quote:

> Hmm- I know I am on _very_ thin ice here, and it's a question, not
> an attempt to answer:

> I can't imagine how that could work.
> Struct S may be expanded at it's end with some chars.
> struct T may be expanded with some struct's S at it's end, where
> every struct S could be of different size.
> I have no idea how an implementation could manage that.

I was under the impression that the variable length arrays were really just
pointers; so the size of each structure would be static.  The size of the
memory block that the pointers point to would change, but that actually
struct size should be the same.

-Will
--



Wed, 07 May 2003 03:00:00 GMT  
 Nested flexible array members
Ben Pfaff schrieb:

Quote:


> > Can the base type of a flexible array member be a structure that
> > contains a flexible array member?

> No.  C99 section 6.7.2.1:

>         ...the last member of a structure with more than one
>         named member may have incomplete array type; such a
>         structure... shall not be a member of a structure or an
>         element of an array.
> --


Thank you, Ben I was searching for that, but could not find it (I
have only the draft in pdf since some days and searching there is
hard if you don't know the exact phrase)

Kind regards
Robert

--

I don't make my mistakes more than once. I store them carefully and
after some time I take them out again, add some new features and
_reuse_ them.
--



Wed, 07 May 2003 03:00:00 GMT  
 Nested flexible array members
Quote:

> Can the base type of a flexible array member be a structure that
> contains a flexible array member?  For example, would the following be
> legal?

No, it is not legal, it's just as our dear fellows on c.l.c.m mentioned,
the ANSI C99 has prohibited its occasional acceptance by various
compilers in the past - the compiler makers will pick up the notion of an
incomplete type which can not be nested.

You can however try to use an array of length null:

Quote:
> struct S{
>      int i;
>      char c[0];         // <---------
> };

> struct T{
>      int j;
>      struct S s[0];     // <---------
> };

many compilers out there will accept this one as a complete
member-type with an internal sizeof == 0. Still you are on very
thin ice, compilers may reject this one, since it is explicitly
illegal to create a zerowidth object, however you will find many
compilers that accept zerowidth declaration members - I could
not find a portion in the standard that does either of it -
to prohibit it or to require it. However, don't turn on a
bounds checker - a b.s. will accept incomplete array members,
but a b.s. may haunt you for using a zerowidth array member.

cheers,
-- guido                                Edel sei der Mensch, hilfreich und gut

--



Wed, 07 May 2003 03:00:00 GMT  
 Nested flexible array members

Quote:

>I was under the impression that the variable length arrays were
>really just pointers ...

Careful, C99 has two separate concepts: VLAs (which are indeed just
pointers plus compile-time information as to which variables contain
the sizes), and "flexible array members".  The original poster was
talking about the latter.

Flexible array members are a standardized version of the common
trick of writing, e.g.:

        struct blah {
                size_t size;    /* current size of data[] */
                size_t limit;   /* maximum size of data[] */
                char data[1];   /* the data */
        };

        struct blah *newblah(size_t maxsize) {
                struct blah *p;

                p = malloc(sizeof *p - sizeof p->data + maxsize);
                if (p != NULL) {
                        p->size = 0;
                        p->limit = maxsize;
                }
                return p;
        }

Callers would do something like "p = newblah(64)" to get room for
up to 64 bytes of data (p->data[0] through p->data[63]).  This is
officially wrong in C89, though it usually works.  (In fact, I just
recently wrote code that depends on it.  This code also depends on
a bunch of other nonportable tricks though, so I am not too worried
about this one. :-) )
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Wed, 07 May 2003 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Flexible array members and allocation

2. Flexible array member (FAQ 2.6)

3. Array of flexible structures.

4. Flexible array indexing in C (like Pascal)

5. nested class virtual members

6. how to access private member of a nested class

7. Addressing to members of surrounding class from nested class

8. Nested struct array?

9. nested loops/2d-arrays HELP (please)

10. Nested Array of Structures - possible?????

11. Howto initialize a nested class array?

12. nested array

 

 
Powered by phpBB® Forum Software