NO

Author Topic: A bug in handling dynamic arrays in structs  (Read 5694 times)

Romashka

  • Guest
A bug in handling dynamic arrays in structs
« on: July 17, 2006, 10:32:37 PM »
Code: [Select]

typedef struct CFG_WordList
{
    int    n;
    char * list[]; /* main.c(16): error #2139: Too many initializers. */
/*    char * list[3]; - works */
} CFG_WordList;


CFG_WordList VIDEODRIVERS = {
    3,
    {
        "x11",
        "dga",
        "directfb"
    }
};

int main(int argc, char *argv[])
{
    /* ... */
    return 0;
}


The code with list[] works fine in GCC. In Pelles C it works only when specifying the size of array (list[3]).
IMHO this is a bug.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
A bug in handling dynamic arrays in structs
« Reply #1 on: July 18, 2006, 02:18:19 PM »
Not sure is a bug.
Allowing the initialization as you like means to have variable size structures wich is clearly erratic.
I'm surprised of gcc behaviour, I want to test it.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Romashka

  • Guest
A bug in handling dynamic arrays in structs
« Reply #2 on: July 18, 2006, 11:01:50 PM »
This code is legal.
There is a feature called "flexible array member" in C99.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
A bug in handling dynamic arrays in structs
« Reply #3 on: July 18, 2006, 11:55:58 PM »
I made some investigation.
C99 states that the flexible array member is allowed only as last element of a structure. A structure that contains a flexible array member is an "incomplete type" and for this reason has some restrictions:
- could not be included in another structure/union (while a pointer is legal)
- could not be argument of sizeof operator
- could not be statically initialized.

Some compilers (GCC also) have extensions that are not C99 compliant:
- Flexible array members can be declared in any part of a structure, not just as the last member.
- Structures containing flexible array members can be members of other structures.
- Flexible array members can be statically initialized.

Last, C99 requires that the padding element to align flexible structure member is positioned before it, while many compiler position it after. So this means that an object library could not be compatible with code compiled from a different compiler.

So in my opinion this is not a bug, but could be a feature request.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Romashka

  • Guest
A bug in handling dynamic arrays in structs
« Reply #4 on: July 19, 2006, 03:11:27 PM »
Quote from: "frankie"
I made some investigation.
C99 states that the flexible array member is allowed only as last element of a structure. A structure that contains a flexible array member is an "incomplete type" and for this reason has some restrictions:
- could not be included in another structure/union (while a pointer is legal)
- could not be argument of sizeof operator
- could not be statically initialized.

I don't understand the reason of the last restriction. :?

Quote from: "frankie"
Some compilers (GCC also) have extensions that are not C99 compliant:
- Flexible array members can be declared in any part of a structure, not just as the last member.
- Structures containing flexible array members can be members of other structures.
- Flexible array members can be statically initialized.

I like these extensions, except the first one.

Quote from: "frankie"
Last, C99 requires that the padding element to align flexible structure member is positioned before it, while many compiler position it after. So this means that an object library could not be compatible with code compiled from a different compiler.


This is not a requirement now.
See Defect Report #282 - it was added to TC2 for ISO/IEC 9899:1999 so it is in C99 now.

Quote from: "frankie"
So in my opinion this is not a bug, but could be a feature request.

OK, so let it be a feature request! ;) Else I had to rewrite some parts of my library to workaround this. :(
My feature request is only to support static initialization, not all GCC's extensions.

PS: Thank you very much for investigation and clarification. I think it is not critical for me and others to have this feature implemented, so if it is hard to implement (bug-free of course) - it not worth it.

PPS: What about other projects? Can some of them be implemented before officially included in C99?
I'm particullary interested in TR 24731: Safer C library functions and TR 24732: Decimal floating point.

kalikiana

  • Guest
A bug in handling dynamic arrays in structs
« Reply #5 on: July 19, 2006, 07:55:04 PM »
Personally I prefer mostly standards compliant code to using inoffical and porprietary extensions. I don't know what you are usually developing, but if you now came from GCC to PellesC and you are going to use extensions only available in PellesC or as you said features which are not yet included in the standard, will you one day ask the GCC guys to support PellesC extensions?
I don't know how much you're using proprietary syntax, but in my opinion compile performance and good support for current standards come prior to any extras.

Romashka

  • Guest
A bug in handling dynamic arrays in structs
« Reply #6 on: July 19, 2006, 09:50:12 PM »
I prefer standards compliant code too. I'm 'web evangelist' and code all my web sites and applications in valid XHTML & CSS only (plus some cross-platform JavaScript where it is really needed).
For my C development I used ANSI C most of the time. I just didn't know that static initialization of flexible array member is prohibited in C99.
Of course I will modify my code that used this feature (though I don't understand completely why such restriction was imposed) so it can be compiled on most platforms and compilers, because I always want to write as portable and standards compliant code as possible.
I like Pelles C because of standards compliance and absence of C++ support.
BTW it is sometimes hard to choose which C standard to use - old ANSI C or new ISO C99 (which was however accepted by ANSI in 2000). Sometimes I need to sacrifice code clarity and functions to portability.

kalikiana

  • Guest
A bug in handling dynamic arrays in structs
« Reply #7 on: July 19, 2006, 10:08:53 PM »
Quote from: "Romashka"
[...]
BTW it is sometimes hard to choose which C standard to use - old ANSI C or new ISO C99 (which was however accepted by ANSI in 2000). Sometimes I need to sacrifice code clarity and functions to portability.


I do like some of the C99 features. However, I would always try to compile with BCC5, MingW and GCC from time to time and therefore mostly stick to ANSI C.