I have a report, unconfirmed by me but from a reliable source, that the code
is compiled by a modern version of gcc as if it had been written
Evidently it is believed that calling with is undefined behavior.
[Edit: The whole premise here was found to be false; see "Update 2" below.]
It has been pointed out that Posix explicitly blesses the case, but evidently no extant version of the C Standard does.
So the obvious questions are:
Is calling with actually undefined behavior in C?
Is every program which ever calls with arbitrary truly obliged to check for and not call in that case?
Why would gcc perform this "optimization"? Even if you believe that calling with is undefined, this would seem to marginally slow down every non undefined program.
Textbook implementations of quicksort (which, I know, is not required to be) pretty much can't not handle correctly. I wonder if gcc's behavior here is trying to guard against a implementation that somehow does something much worse than a if the initial call has ?
Update: Thanks for the responses so far. It sounds like gcc is in the wrong here. As I said, I haven't confirmed this result myself, but I'm trying to find out which version of gcc and with which optimization flags the issue was observed.
Update 2: The original report I referred to was in error. Two key clarifications:
gcc was in fact checking for , not . This is obviously a completely different kettle of fish: As this thread (and others) has confirmed, calling on a null pointer is considerably more problematic, and almost certainly formally undefined.
The compilation in question included the and flags, so of course gcc was being stringent about checking for inadvertent null pointers (and even at a cost to efficiency).
Sorry for the misinformation and runaround. I'm afraid this question is now in the realm of "not reproducible or was caused by a typo", and I have put one close vote in the hopper on that basis.
For what it's worth, the gcc version was 12.2.1.
View on Stack Overflow
