Comparing pointers in C and D

Paul Backus snarwin at gmail.com
Thu Feb 16 06:28:04 UTC 2023


On Thursday, 16 February 2023 at 01:13:17 UTC, Ali Çehreli wrote:
> On 2/15/23 12:07, Olivier Pisano wrote:
>
> > https://pvs-studio.com/en/blog/posts/cpp/0576/
>
> I haven't read the article but the example has the following:
>
>     int a, b;
>     int *p = &a;
>     int *q = &b + 1;
>
> As far as I know, that pointer arithmetic applied to a single 
> object is undefined behavior.

In C the addition itself is defined behavior (you are allowed to 
create a pointer to "one past the end" of an object), but 
dereferencing it would be UB. [1] The D spec does not say 
anything about this, but I think it's safe to assume that it 
inherits C's semantics.

[1] http://port70.net/~nsz/c/c11/n1570.html#6.5.6p8


More information about the Digitalmars-d mailing list