3 ms·
Would you mind elaborating on that? I don't see this behavior with GCC 4.2.1 on OSX or OpenBSD at -O3 when I look at the disassembly.
by silentbicycle 13y ago
Would you mind elaborating on that? I don't see this behavior with GCC 4.2.1 on OSX or OpenBSD at -O3 when I look at the disassembly.
- vinkelhake 13y agoHere's the simplest example I could come up with (it's functionally equivalent to the code OP pasted): int zero1(int* i) { *i = 0; return 0; } int zero2(int* i) { *i = 0; if(i == 0) return 1; return 0; } GCC produces the same code for these two functions. If the 'if' in zero2 is moved to the top of the function, it takes effect. I tried this on GCC 4.8.1. A wonderful site for checking these things is: http://gcc.godbolt.org/ http://gcc.godbolt.org/
- silentbicycle 13y agoExcellent, thank you. Those also compile differently for me, so it probably appeared in a newer version of gcc.
- throwaway_yy2Di 13y agoIt's an optimization, it shows up at -O2.
- dkersten 13y agoWhy put the if in at all then? Regardless of optimisation, it will never be followed - it segfaults if NULL and returns normally if not.
- deleted 13y ago[deleted]
- throwaway_yy2Di 13y agoThis works. Also it does this without a notice, even when I ask for -Wall. I found you can control this with -f{no-,}delete-null-pointer-checks. Apparently it's enabled by -O2. http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Optimize-Options.html#index-fdelete-null-pointer-checks-785 http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Optimize-Options...