Comment #0 by pro.mathias.lang — 2020-04-06T12:24:52Z
```
import std.stdio;
void main () @safe
{
writeln(typeid(int).initializer());
}
```
SEGV because `initiializer` is `@trusted` and returns a slice of length 4 from 0x0 to 0x4.
Comment #1 by r.sagitario — 2020-04-06T18:52:00Z
This is as safe/unsafe as any null pointer:
struct S { int a, b; }
void main() @safe
{
S* s;
s.b = 3;
}
null pointer accesses and anything in the vicinity are not considered a safety problem because they are supposed to cause an access violation, not a memory corruption.
Comment #2 by pro.mathias.lang — 2020-04-07T03:46:20Z
Not quite. It's true the `null` pointers are not considered unsafe, otherwise we wouldn't be able to dereference anything in `@safe` code.
But one of the guarantee that `@safe` code offers is that you cannot create an invalid, non null pointer. That's why, for example, you can't do `arr.ptr`, but you can do `&arr[idx].ptr`.
And if you do:
```
const void* ptr = &typeid(int).initializer()[$-1];
assert(ptr !is null);
writeln(ptr);
```
You create a pointer with value `0x3`, which breaks `@safe`.
Comment #3 by r.sagitario — 2020-04-07T20:38:16Z
As my example tried to demonstrate, you can do this with a null pointer to struct (or class) aswell:
import std.stdio;
struct S { int a, b; }
void main() @safe
{
S* s;
int* p = &s.b;
writeln(p); // 4
*p = 3; // segfault
}
Comment #4 by pro.mathias.lang — 2020-04-08T03:23:45Z