Today I encountered a bug that was quite difficult to find regarding strings. In order for strings to work they must be null-terminated, and this implies that an array of characters can contain a string with a length equal to the array size minus one, because there must be space for the null character. I found out that, when initializing array of chars with strings, the compiler does not complain if just the null character doesn’t fit.
It’s often a good strategy when developing software to ensure that a number of compiler warnings are switched on. For example, the GCC compiler flag ‘-Wall‘ is often used to provide warnings about all ‘questionable constructions’ (things like omitting to include a return statement in a function that should return a non-void type). Enabling warnings such as this can speed up development as more time is spend debugging application logic rather than silly errors. However the downside is that the compiler may provide warnings about things that we know about and are happy with. This post revisits GCC attributes to see how we can supress such warnings.