Is D's pointer subtraction more permissive than C (and C++)?
Elronnd
elronnd at elronnd.net
Sat Apr 2 09:38:57 UTC 2022
On Friday, 1 April 2022 at 17:39:24 UTC, Steven Schveighoffer
wrote:
> In practice, I don't see how it affects the behavior *of the
> compiler*. When you subtract two pointers, I don't see how the
> compiler/optimizer can make some other decision based on the
> subtraction not being between two pointers to the same block of
> memory.
Unfortunately, they can and do. For instance, consider this
snippet of c code:
#include <stddef.h>
#include <stdlib.h>
int f() {
int *x = malloc(1), *y = malloc(1);
ptrdiff_t d = y - x;
return y == x + d;
}
GCC compiles this to the same code as:
int f() { return 1; }
This is intertwined with issues of provenance.
More information about the Digitalmars-d
mailing list