Re: [cc65] Another wish: Unsized Arrays in Structures

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-01-29 16:43:34


Hi!

On Tue, Jan 28, 2003 at 10:35:27AM +0100, Spiro Trikaliotis wrote:
> > There is a feature called "flexible array members" in ISO/IEC 9899:1999 (E),
> > is it that what you mean?
>
> Don't know the standard, but it may be Christian's wish, yes.

After checking the given URL, I found out that Microsofts implementation
differs from the standard. The feature called "flexible array members" does
legalize a common practice that is also used frequently in the cc65 compiler
sources:

        struct foo {
            int x, y;
            char name[1];
        };

        ...
        struct foo* f = malloc (sizeof (foo) + strlen (name));
        ...

The difference is that the standard does not allow initialization for flexible
array members, while Microsoft allows this.

        struct foo {
            int x, y;
            char name[];
        };

        struct foo f = { 0, 0, "foo" };   /* Illegal in C99, works in MSC */

> I have seen this feature on other plattforms for embedded controllers long
> before Microsoft used it, so I think you should not blame MS.

I'm still not decided how useful this feature actually is. I've been using
structures with an "open" array myself, but always in conjunction with dynamic
memory. The new "flexible array member" thing in C99 allows to express
explicitly what many people were doing before, so I think it's a good thing.
But how useful is this additional Microsoft feature of allowing
initialization? Especially since it introduces some problems. One is that the
sizeof operator does no longer return the actual size of the variable:

        struct foo {
            char x, y;
            char name[];
        };

        struct foo f = { 0, 0, "foo" }; /* Allocate 6 bytes in MSC */
        int size = sizeof (f);          /* size is now 2, not 6! */

Another problem is, that one can initialize single struct instances, but not
arrays of structs:

        struct foo {
            char x, y;
            char name[];
        };

        /* The following code does not work, even in MSC */
        struct foo f[2] = {
            { 0, 0, "foo" },
            { 1, 1, "bar" },
        };

I think that these problems were the reason why C99 does not allow
initialization of the last struct element.

Can anyone post an example where the additional initialization is not only
convenient, but helps to to something not possible before, or were the
alternative, standard compliant code is a lot more complex and/or ugly?

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-01-29 16:43:41 CET