MrBean

const struct with unsized array messed up by compiler, please verify in newest version.

Discussion created by MrBean on May 31, 2011
Latest reply on May 31, 2011 by MrBean
typedef struct { char bNum1; char bNum2; char String[];} tMyStruct;const tMyStruct MyConstStruct = { 0xFF, sizeof(tMyStruct), 'A'};

 

 

Compiles to:

 

   *** INITIALIZED DATA (.rodata) ***Header:  Section Alignment : 1  Section Size      : 30x00000000                _MyConstStruct:0x00000000:  FF 02 41                                         '..A'

 

So far, so good.

 

But:

 

typedef struct { char bNum1; char bNum2; char String[];} tMyStruct;const tMyStruct MyConstStruct = { 0xFF, sizeof(tMyStruct), "ABCDEFGH"};

 

Compiles to:

 

   *** INITIALIZED DATA (.rodata) ***Header:  Section Alignment : 1  Section Size      : 110x00000000                _MyConstStruct:0x00000000:  41 42 43 44 45 46 47 48 00 64 29                 'ABCDEFGH.d)'

 

 

As can be seen in the first example, the 'sizeof' an unsized array is zero, so the sizeof the struct is 2. That is correct.

Anything larger than 1 character moves the string forward in the struct and messes up the rest. That is not correct.

The rubbish at the end of the struct changes when the length of the string is varied.

No errors or warnings in both examples.

 

This is with CW 7.2

Could someone verify this behavior with the newest CW (10.1 ?) please ?

Looks like a bug.

Outcomes