Null pointers, one month later
Null pointers, one month later
Posted Aug 20, 2009 10:35 UTC (Thu) by etienne_lorrain@yahoo.fr (guest, #38022)In reply to: Null pointers, one month later by xilun
Parent article: Null pointers, one month later
In fact the C language doesn't know the identifier "NULL", it just knows that its value is zero because the preproceessor defines that.
You can ask the compiler not to optimise tests against zero by a compilation switch, like it is done in the latest Linux source.
Another solution is to define NULL as an external pointer, and let the linker set its value to zero (either linker command file or ld parameter).
Then, the compiler cannot optimise tests against a value it doesn't know, namely NULL - but it will still optimise away tests against zero, some of them are obvious.
The real problem is to tell the compiler not to optimise the NULL test of this function in the general case:
inline void fct (unsigned *cpt) { if (cpt != NULL) *cpt += 1; }
but to optimse it when it is called as:
static unsigned cpt1; // cpt1 address known not to be zero
voit fct1 (void) { fct (&cpt1); }
or when called as:
void fct2 (void) {
unsigned cpt2; // cpt2 address known not to be zero
fct (&cpt2);
}
That is difficult to acheive.