10 April 2013

I planned to check more information about compilation assertion in C. How to check for size errors, alignments during compilation phase? The answer is compilation assertion which declares a variable which causes compilation error if the condition is true. Here are "simplest" one:

Checking sizeof at compile time [scaryreasoner.wordpress.com]
This article provides explanation of BUILD_BUG_ON macro which is used in linux for example. It can't be used in global scope because it's a statement.

Catching errors early with compile-time assertions [embedded.com]
Dan Saks article which solves the global scope problem with previous one,

#define compile_time_assert(cond, msg) \
typedef char msg[(cond) ? 1 : -1]

Even though message is not shown in some IDEs, at least code line is thus it's easy to decipher it from the msg parameter why it caused an error.


blog comments powered by Disqus