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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

695 Views
MrBean
Contributor I
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.

Labels (1)
0 Kudos
1 Reply

431 Views
MrBean
Contributor I

PS.: This does work:

 

typedef struct { char bNum1; char bNum2; char String[];} tMyStruct;const tMyStruct MyConstStruct = { 0xFF, sizeof(tMyStruct), { 'A','B','C','D','E','F','G','H','\0' }};

 

Compiles to:

 

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

 

 

Should be no different from example #2 in first post though ...

0 Kudos