Anyone want to guess what gcc generates for a==b?0:a-b ? Any ideas why?
-
Show this thread
-
The expression a==b?0:a-b is not stupid, btw. It's essentially the only correct way to write a-b when a and b are pointers and both being null is a possibility you want to allow for.
3 replies 3 retweets 6 likesShow this thread -
Replying to @RichFelker
why is null special? If it's doing stupid pointer shenanigans, typecast both to long.
2 replies 0 retweets 0 likes -
Replying to @landley
BTW even if you want to assume memory model, (long)a-(long)b is not a safe way to subtract pointers. Consider a=0x7fffffff and b=0x80000000. (long)a-(long)b overflows; if you force that to be defined (-fwrapv) then you can't detect other (bad) overflows with ubsan.
1 reply 0 retweets 0 likes -
-
-
Replying to @RichFelker
The code in question is calculating stack usage to figure out if it should recurse or exec, both pointers on same stack.
1 reply 0 retweets 0 likes -
Replying to @landley
OK, you're bringing in something completely separate from the original topic here. If you want to measure distance in address space for some reason related to the address space model, casts to an integer type are the right way because subtracing non-same-array ptrs is undefined.
1 reply 0 retweets 0 likes
The topic of the thread you were replying to was subtracting pointers that are either both pointers into the same array, or both null, and you want the result to be 0 in the latter case.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.