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,739 次查看
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 解答
759 次查看
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 回复数
760 次查看
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 项奖励
回复
759 次查看
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 项奖励
回复