Compile-time check of structure-size on CW S12 v. 5.0

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

Compile-time check of structure-size on CW S12 v. 5.0

ソリューションへジャンプ
1,660件の閲覧回数
Sten
Contributor IV

My application has several external (I2C) EEPROMs and I have defined the data to store in them as an own stucture-typedef for each EEPROM. Because the sizes of the structures varies when I am developing the software, it is possible that a structure gets too big to fit in it's EEPROM. Is there a way to build a compile-time check for this (so that I get a compiler-error if a structure is too big)? The sizeof-operator does not work in preprocessor-directives so I can't use a "#if sizeof(i2c1_struct) > 0x7fff"-directive.

 

Any suggestions?

 

Sten

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
680件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

One way is to generate an error when the condition is not met. The main problem is if you can get an

error an unaware developer can detect on his own what the problem was.

I did use something like this in the past:

 

 

#define COMPILE_TIME_ASSERT(cond) extern int CompileTimeTest[(cond) ? 10 : 9]extern int CompileTimeTest[10];

 

It redeclares an array with a different numbers of elements, using the array name as first hint of the cause. And a lot of comments at the declaration location as second hint.

 

For the given case of placing a structure in memory I wonder if not the linker should actually warn about the problem? Maybe not if the external EEPROM is not known.

 

Daniel

 

元の投稿で解決策を見る

0 件の賞賛
返信
2 返答(返信)
681件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

One way is to generate an error when the condition is not met. The main problem is if you can get an

error an unaware developer can detect on his own what the problem was.

I did use something like this in the past:

 

 

#define COMPILE_TIME_ASSERT(cond) extern int CompileTimeTest[(cond) ? 10 : 9]extern int CompileTimeTest[10];

 

It redeclares an array with a different numbers of elements, using the array name as first hint of the cause. And a lot of comments at the declaration location as second hint.

 

For the given case of placing a structure in memory I wonder if not the linker should actually warn about the problem? Maybe not if the external EEPROM is not known.

 

Daniel

 

0 件の賞賛
返信
680件の閲覧回数
Sten
Contributor IV

Thanks Daniel, a very creative solution and works very fine. The error message is a bit cryptic, but as you said, that can be solved with good commenting in the source.

 

Sten

 

0 件の賞賛
返信