memory safety checks and trust

Walter Bright newshound2 at digitalmars.com
Tue Apr 14 10:08:51 UTC 2020


On 4/13/2020 5:25 AM, Steven Schveighoffer wrote:
> For instance:
> 
> void foo()
> {
>      int*[] b;
>      int a;
>      b ~= &a;
>          // use b but don't expose it outside foo
> }
> 
> There are no memory safety violations there.
> The first case is highly useful, as one often needs scratch space to perform 
> complex calculations or graph algorithms. I see no reason to disallow it.

I see a reason:

  void foo()
  {
     int*[1] b = void;
     int a;
     b[0] = &a;
  }

It's faster, too. And if it is written as:

  &safe void foo()
  {
     int a;
     int*[1] b;
     b[0] = &a;
  }

it's even @safe (with -preview=dip1000).

I know, I know, this isn't the real use case. But I've done plenty of "use 
scratch data structures on the stack" programming for speed and I know how to 
make it work without needing to store addresses in GC objects.


More information about the Digitalmars-d mailing list