c - Initialize all variables, always -
i reading the freebsd coding style , quite liking (as vertically compact code). there this:
initialize variables
shall initialize variables. always. every time. gcc flag -w may catch operations on uninitialized variables, may not.justification
more problems can believe traced pointer or variable left uninitialized.
when there no appropriate initial value variable, isn't better leave without value. way compiler catch reading uninitialized. not talking t *p = null
, trap representation , might (or may not) quite useful, rather int personal_number = 0 /* 0 valid personal number!!*/
to clarify, in response abasu's comment, my example trying illustrate cases when there no available invalid values. have asked question , answered using impossible values mark errors or other conditions awesome. not case. examples plentiful: 8bit pixel value, velocity vector, etc.
one valid alternative "always initialize variables", can see is:
//logical place declarations t a; /*code, example set environment evaluating a*/ = foofora(); /*more code*/ foothatusesa(a);
this way if initialization forgotten, there warning , bug fixed, removing warning.
are all integers valid personal numbers?
if not, use invalid value initialize personal_number
.
if are, when have not initialized personal_number
it still holds value valid personal number -- value unknown. initialize 0
anyway -- have not introduced problem (valid number before, valid number after), difference number known you.
of course in both cases better not use integer literal initialization, rather this:
enum { invalid_personal_number = -1 } int personal_number = invalid_personal_number;
Comments
Post a Comment