http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54454

             Bug #: 54454
           Summary: gcc violates c99 specification w.r.t. flexible arrays
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: miku...@artax.karlin.mff.cuni.cz
              Host: hppa-linux-gnu
            Target: hppa-linux-gnu
             Build: hppa-linux-gnu


The c99 specification says (section 6.7.2.1, paragraph 16): "The size of the
structure [with flexible array member] 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". This is not true in gcc.
Try to complile and run this program: the size of the structure with the
flexible array member is 8. However, offset of the flexible array member is 6.
According to the specification, sizeof(struct flexible) should be 6.

#include <stdio.h>
#include <stddef.h>

struct flexible {
        unsigned a;
        unsigned short b;
        unsigned char array[];
};

struct not_flexible {
        unsigned a;
        unsigned short b;
        unsigned char array[1];
};

int main(void)
{
        printf("sizeof(struct flexible) = %d\n", (int)sizeof(struct flexible));
        printf("offsetof(struct not_flexible, array)) = %d\n",
(int)offsetof(struct not_flexible, array));
        printf("offsetof(struct flexible, array)) = %d\n", (int)offsetof(struct
flexible, array));
        return 0;
}

Reply via email to