3 ms·
Always: import std.c.stdio; int main() { int x = 7; int* p = &x; printf ("%d\n", *(p + 1)); return 0; } This compiles in D and violates memo
by stonemetal 10y ago
Always:
import std.c.stdio;
int main() {
int x = 7;
int* p = &x;
printf ("%d\n", *(p + 1));
return 0;
}
This compiles in D and violates memory safety.
If you prefer a documentation answer(https://dlang.org/spec/arrays.html#pointers https://dlang.org/spec/arrays.html#pointers)
These are simple pointers to data, analogous to C pointers. Pointers are provided for interfacing with C and for specialized systems work. There is no length associated with it, and so there is no way for the compiler or runtime to do bounds checking, etc., on it.
- WalterBright 10y agoAdd '@safe' to main()'s declaration and it will no longer compile.
- deleted 10y ago[deleted]
- notriddle 10y agoimport std.stdio; @safe int f() { int* x = new int(6); delete x; // !! return *x; // !!!! } @safe void main() { writeln(f()); }
- nialv7 10y ago'delete' is in the process of being deprecated: https://dlang.org/deprecate.html#delete https://dlang.org/deprecate.html#delete