On 14/06/2021 06:15, Xi Ruoyao wrote: > On Sun, 2021-06-13 at 23:30 +0100, Jonny Grant wrote: >> Hello >> >> This isn't real code, just an example to show. >> >> I've tried with: -Wall -Wextra -O2 and some other warnings, but >> couldn't get this to generate a warning that *g was possibly de- >> referenced. May I ask, does GCC have a way to get warnings when pointers >> are not checked? >> I had a look but -Wnull-dereference didn't help. >> >> #include <stdlib.h> >> >> #include <cstddef> >> void f(int * g) >> { >> *g = 1; >> >> if(NULL == g) >> { >> exit(1); >> } >> } >> >> Best regards Jonny > > It was explained by Chris Lattner at > http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html > > GCC (<= 4.4) had -Wunreachable-code which might work for this case. But > it was too unreliable (as Chris said, generally there is no reliable way > to do this) and removed in later releases. > Thank you for your reply and the link. And for Martin's reply. I guess a separate static analyser would do it, GCC is more focused on compilation so I shouldn't ask for it to have so many features it can't support. Chris Latner also mentioned integer overflow being undefined, that crops up too. There's no easy solution right, we need to hand write code the checks? It's human-error prone if we need to manually code each check. throwing in C++, or handling in C. if(N >= INT_MAX) { throw std::overflow_error("N >= INT_MAX would overflow in for loop"); } for (i = 0; i <= N; ++i) { // ... } Cheers, Jonny