Comparing pointers in C and D

Ali Çehreli acehreli at yahoo.com
Thu Feb 16 01:13:17 UTC 2023


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.

(I would love to learn otherwise if I am mistaken there. But I've just 
checked again, an expression like '&b + 1' can only be applied to 
pointers into array elements. Since b is not an array element, I am 
confident that it's undefined behavior.)

If so, I think both gcc and llvm are aware of undefined behavior and it 
looks trivial to catch the one above. If they detect undefined behavior, 
they can produce any result. But I don't know whether they apply that 
logic. (?)

Ali



More information about the Digitalmars-d mailing list