Originally Posted by
CornedBee
Not in the way you expected, but look at 6.5.8.5, which defines the semantics of relational operations on pointers.
Ta, CornedBee. It made sense to me that comparing a free'd pointer with something wouldn't be defined. I read that section, but my mind must have drifted; one of the hazards of reading a standard, I guess.
It would be a slightly interesting exercise to see if we can find a real compiler/library that, when it encounters this code...
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a, *b, *temp;
temp = malloc(2*sizeof(int));
a = temp;
b = temp + 1;
free(temp);
if (b > a) fprintf(stdout, "Yes!"); /* undefined behaviour here */
return 0;
}
does anything other than print "Yes!".