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
已解决! 转到解答。
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
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