On Sat, Jul 01, 2017 at 02:23:36AM +0200, ag0aep6g via Digitalmars-d-learn 
wrote:
> On 07/01/2017 01:41 AM, bauss wrote:
[...]
> >      stringSize, &bytesRead)) {
> >      return defaultValue;
> >    }
> > 
> >    auto s = cast(string)data[0 .. stringSize];
> > 
> >    return s ? s : defaultValue;
> 
> Here's an error that produces garbage.
> 
> `data` is a fixed-sized array, so the values are on the stack. That
> means `s` points to the stack. You can't return a pointer to the
> stack. It becomes invalid when the function returns. You can put it on
> the heap instead: `auto s = data[0 .. stringSize].idup;`.
[...]

The compiler is supposed to catch errors like these with -dip1000.
Recently there was a DIP1000-related fix checked in that fixed some of
the problems with -dip1000 (specifically, a linker error I was running
into), so you may want to consider compiling with -dip1000 if you're
running the latest compiler.


T

-- 
Klein bottle for rent ... inquire within. -- Stephen Mulraney

Reply via email to