On 11/06/2021 10:44, Xi Ruoyao wrote: > On Fri, 2021-06-11 at 10:37 +0100, Jonny Grant wrote: >> Hello >> This isn't real code, it's just an example to ask this question: >> >> Would GCC optimizer ever re-order these statements and cause a NULL ptr >> de-reference SIGSEGV? I recall reading a Chris Lattner paper indicating >> it could happen. >> >> void f(int * p) >> { >> if(!p) >> { >> return; >> } >> >> printf("%d\n", *p); >> } > > No it won't. > > What Chris paper says is about something like: > > void f(int * p) > { > int x = *p; > > if(!p) > { > return; > } > > printf("%d\n", x); > } > > if p is NULL, this is an UB and the compiler can do anything. > Many thanks for your reply Xi. Yes, I see, the compiler wouldn't change the order. Jonny