D not considered memory safe
Sebastian Nibisz
snibisz at gmail.com
Thu Jul 4 07:11:13 UTC 2024
On Thursday, 4 July 2024 at 02:00:52 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> ``-preview=dip1000``
>
> ```d
> @safe:
>
> ref int bar(ref int x) => x;
>
> ref int foo()
> {
> int i = 0;
> return bar(i);
> }
>
> void main() {
> int* v = &foo();
> }
> ```
>
> ```
> <source>(3): Error: returning `x` escapes a reference to
> parameter `x`
> <source>(3): perhaps annotate the parameter with `return`
> ```
>
> And when annotated:
>
> ```
> <source>(8): Error: returning `bar(i)` escapes a reference to
> local variable `i`
> ```
An unsafe programming language is one that allows to write unsafe
code by default. If D is to be more safe, then @safe should be
the default and @unsafe optional.
More information about the Digitalmars-d
mailing list