#254 FATAL Compiler Error when using array[]

closed-fixed
Sandeep Dutta
5
2013-05-25
2001-12-14
Alex Karahalios
No

I get the following error

main.c:4: error: FATAL Compiler Internal Error in file 'SDCCsymt.c' line number '790' : can not tell the size of an array[]
Contact Author with source code

on this code

typedef struct _S_ {
unsigned char A;
unsigned char B[];
} S;

The ANSI C spec allows for an indeterminate array as the last array of a structure. You should also be able to use an indeterminate array as a union member.

I make heavy use of this in my current project.

Discussion

  • Logged In: YES
    user_id=361551

    I forgot to mention that this is for the latest CVS version of SDCC. This is not a problem with previous versions.

     
  • Johan Knol
    Johan Knol
    2001-12-19

    Logged In: YES
    user_id=63512

    gcc tells me: "field 'B' has incomplete type, so I think
    this is ok.

    typedef struct _S_ {
    unsigned int A;
    unsigned char B[1];
    } S;

    S *packet;

    void test (void) {
    packet->B[packet->A]=4;
    }

    Should do it.

     
  • Johan Knol
    Johan Knol
    2001-12-19

    • milestone: --> 100455
    • status: open --> closed-rejected
     
  • Logged In: YES
    user_id=361551

    GCC is not an indication of ANSI compliance. Here is the relevant section of the ANSI C99 manual (section 6.7.2.1 Structure and union specifiers):

    ----
    "15 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 the size of the
    structure shall be equal to the offset of the last element of an otherwise identical structure
    that replaces the flexible array member with an array of unspecified length.95) When an
    lvalue whose type is a structure with a flexible array member is used to access an object,
    it behaves as if that member were replaced with the longest array, with the same element
    type, that would not make the structure larger than the object being accessed; the offset of
    the array shall remain that of the flexible array member, even if this would differ from
    that of the replacement array. If this array would have no elements, then it behaves as if it
    had one element, but the behavior is undefined if any attempt is made to access that
    element or to generate a pointer one past it.

    16 EXAMPLE Assuming that all array members are aligned the same, after the declarations:

    struct s { int n; double d[]; };
    struct ss { int n; double d[1]; };

    the three expressions:

    sizeof (struct s)
    offsetof(struct s, d)
    offsetof(struct ss, d)

    have the same value. The structure struct s has a flexible array member d."
    ----

    Now that said, GCC does allow you to have a 0 sized array. So that

    typedef struct _S_ {
    unsigned int A;
    unsigned char B[0];
    } S;

    is valid in GCC, but produces the an error in SDCC.

     
  • Sandeep Dutta
    Sandeep Dutta
    2001-12-20

    • labels: --> C-Front End
    • milestone: 100455 --> fixed
    • assigned_to: nobody --> sandeep
    • status: closed-rejected --> closed-fixed
     
  • Sandeep Dutta
    Sandeep Dutta
    2001-12-20

    Logged In: YES
    user_id=1551

    fixed, allows 0 byte arrays in sturctures but in globals