D defined behavior

Luís Marques luis at luismarques.eu
Mon Apr 27 14:07:49 UTC 2020


I've lost track. Does D have undefined behavior at all? (e.g. 
outside @safe code).

In any case, I imagine that the D spec (ha!) would dictate that 
the following code should print 0x10001, although that's not 
quite clear.

GDC (optimized) prints 0x1.
LDC2 prints 0x10001, but I suspect only because LLVM doesn't 
happen to do the same optimization, not because this is handled 
properly.

```
import std.stdio;

@safe:

int foo(int* a, int* b) {
     *a = 1;
     *b = 1;
     return *a;
}

void main() {
     ubyte[6] buffer;
     auto a = cast(int[]) (buffer[0 .. 4]);
     auto b = cast(int[]) (buffer[2 .. 6]);
     writefln("0x%X", foo(&a[0], &b[0]));
}
```


More information about the Digitalmars-d mailing list