More evidence that memory safety is the future for programming languages

Walter Bright newshound2 at digitalmars.com
Thu Apr 2 20:23:56 UTC 2020


On 4/2/2020 3:04 AM, Jacob Carlborg wrote:
> $ clang --analyze main.c
> main.c:2:19: warning: Address of stack memory associated with local variable 'i' 
> returned to caller
> int* foo(int i) { return bar(&i); }
>           ~~~~~    ^~~~~~~~~~~~~~
> 1 warning generated.

Now try:

   int* bar(int* p);
   int* foo(int i) { return bar(&i); }

And then:

   struct S { int* p; };

   struct S foo(struct S* ps, int i)
   {
       ps->p = &i;
       return *ps;
   }

It falls apart. Now let's try D:

   struct S { int* p; }

   @safe S foo(S* ps, int i)
   {
       ps.p = &i; // Error: cannot take address of parameter i in @safe function foo
       return *ps;
   }

The point is to get them all, not a few simple patterns.


More information about the Digitalmars-d mailing list