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

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

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

1,258件の閲覧回数
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.

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 返信

994件の閲覧回数
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 件の賞賛
返信