On 12/14/2013 10:48 AM, Gary Willoughby wrote:
> On Friday, 13 December 2013 at 17:35:27 UTC, John Colvin wrote:
>>> public void opAssign(uint value)
>>> {
>>> this._octets = value.nativeToBigEndian();
>>> assert(this._octets == [1, 2, 3, 4]);
>>> }
>>> }
>> opAssign is escaping a reference to its stack by assigning the static
>> array to the slice _octets. Therefore, garbage.
>
> I'm not going to lie that has gone over my head a little. Could you
> explain it more simply please? I just want to totally understand the
> issue here. Thanks.
According to documentation, nativeToBigEndian returns a static array
(fixed-length array):
http://dlang.org/phobos/std_bitmanip.html#.nativeToBigEndian
It says "returns it as a ubyte[n] where n is the size of the given type."
Since static arrays normally live on the stack, the returned array is a
local array.
As with any slice assignment, the assignment to this._octets in opAssign
makes _octets a slice to the elements of that local array. Upon leaving
opAssign that array is no more. So, _octets is left referring to
elements that are long gone. :(
Ali