incorrect data when returning static array in place of dynamic

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 5 22:30:44 PDT 2015


On Sunday, 5 July 2015 at 18:57:46 UTC, sigod wrote:
> Why does function return incorrect data? Using `.dup` in return 
> expression or using `ubyte[20]` as return type fixes problem, 
> but why?

Because sha1Of() returns ubyte[20], this is a stack-allocated 
array, a value type. If you put correct return type there, it 
will be returned by value and everything's fine. If your return 
type is ubyte[] (a reference type), a slice of stack-allocated 
array is returned which creates a reference to stack data that 
doesn't exist anymore.


More information about the Digitalmars-d-learn mailing list