4 ms·
The problems are that: - NULL is idiomatic: using NULL is entrenched in C programming and it is not going away. - In spite of nullptr existing now, NULL is st
by kazinator 4y ago
The problems are that:
- NULL is idiomatic: using NULL is entrenched in C programming and it is not going away.
- In spite of nullptr existing now, NULL is still (quite stupidly) not required to just expand to nullptr, but to an implementation-defined null pointer constant, rather than #define NULL nullptr. (According to the N2596 draft).
- They had over 30 years to tighten the requirements on how NULL can be defined; what's the matter? C99 could already have required NULL to be ((void *) X) where X is an integer-typed constant expression evaluating to zero.
I'm not going to start using nullptr. It's not idiomatic C. I'm going to hold out hope that NULL will be fixed so that it expands to nullptr.
--
Also, it's possible for a compiler to diagnose when a constant, zero-valued expression is used as the argument of a variadic function. The diagnostic can be confined to cases when such a constant expression is the result of macro expansion:
printf_like_function("fmt ...", ... 0, ...); // OK
#define FOO 0
printf_like_function("fmt ...", ... FOO, ...); // compiler diagnostic
printf_like_function("fmt ...", ... (int) FOO, ...); // OK
- bigbillheck 4y ago> NULL is idiomatic It is today, but idioms are a human concept and who knows how things will be in 2033?
- kllrnohj 4y agoWell hopefully at some point we'll stop writing C at all
- bigbillheck 4y agoInshallah some day, but I don't see it happening in what's left of my career.
- pjmlp 4y agoOnce upon a time K&R C function declarations were idiomatic, in C23 they are out.
- cogman10 4y ago> In spite of nullptr existing now, NULL is still (quite stupidly) not required to just expand to nullptr, but to an implementation-defined null pointer constant, rather than #define NULL nullptr. (According to the N2596 draft). This is so silly. I sort of get why not (can't break the dork that decided to do int i = NULL; i++; ) But, at the same time... I almost feel like this is a "you are being a dork, go fix your code." moment. This isn't the sort of break where someone would see it and go "Oh yeah, assuming NULL is anything other than nullptr is dumb!"
- kazinator 4y ago> can't break the dork that decided to Why not? We've broken the dork who used undeclared functions, void main, gets ... (It's the same funking dork anyway. You know who you are, I'm looking at you!) Note that int x = ((void *) 0); will actually work in GCC and get you a zero into x, just with a conversion warning. The dork is unaffected; their code works and they don't read warnings.
- GabrielTFS 4y agoSee https://gcc.gnu.org/pipermail/gcc/2023-May/241264.html https://gcc.gnu.org/pipermail/gcc/2023-May/241264.html for why this might not be the case anymore soon - although I suppose that adding in -fpermissive or -Wno-error=conversion or something like that isn't too much effort.