static array internal & dangling reference

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 13 23:08:56 PST 2016


On Monday, November 14, 2016 06:19:25 Picaud Vincent via Digitalmars-d-learn 
wrote:
> On Monday, 14 November 2016 at 06:10:38 UTC, Jonathan M Davis
>
> wrote:
> > I would have hoped that it would have complained about the
> > first one. I don't know why it isn't. It definitely results in
> > having a pointer to memory that should no longer be referenced.
>
> Yes I would have hoped too, because it has the unfortunate
> consequence that we can not rely 100% that there is no dangling
> pointer even if valgrind said OK.

Well, valgrind is good, but I think that it would be a mistake to assume
that it's perfect. Ideally, this should probably be reported to them, but
you'd probably have to come up with equivalent C/C++ code that had the
problem to report it.

> > Regardless, there's no question that returning a dynamic array
> > which is a slice of a static array is not something that is
> > valid to do when that static array is a local variable.
> > Valgrind just isn't managing to catch it in this case,
> > unfortunately.
>
> I do agree, this is not a construct to use, I only have done this
> for testing purpose to see how D reacts.

This is something that is _supposed_ to be caught by the compiler and
flagged as @system so that if you marked your code as @safe, the compiler
would complain about it, but there's a longstanding bug in the compiler that
this isn't caught. However, Walter has been putting in a fair bit of work
lately to improve @safe, so I expect that there's a good chance that it will
be fixed at some point in the near future.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list