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

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

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

Jump to solution
1,487 Views
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

Labels (1)
Tags (1)
0 Kudos
1 Solution
507 Views
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

 

View solution in original post

0 Kudos
2 Replies
508 Views
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 Kudos
507 Views
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 Kudos