I have a question about structures with zero length arrays. Lets start with the example code:

struct {
 char a;
 char b[];
} s = {
 'a',
 "bc"
};

char a[] = { 'a', 'b', 'c' };

int
main(void)
{
 printf("%d %d\n",sizeof(s),sizeof(a));
 return 0;
}

When compiled with gcc the result is 1 3. I expected 3 3.
The assembly file looks like (x86):

.globl s
       .data
       .type   s, @object
       .size   s, 1
s:
       .byte   97
       .string "bc"
.globl a
       .type   a, @object
       .size   a, 3
a:
       .byte   97
       .byte   98
       .byte   99

So here the sizeof(s) is also 1. This looks wrong to me as well.
I can not find any refence in the gcc.info file about the sizeof an initialized
structures with zero length arrays. I can find it is a gcc extention.
Is there some one who knows what the result of sizeof(s) should be
and also if the assembly code is correct or not?
If found this problem when implementing the bounds-checking code for
gcc-4.0.0.


Herman.

Reply via email to