ubyte array changing values between calls? Possible bug?

John Colvin john.loughran.colvin at gmail.com
Fri Dec 13 09:35:25 PST 2013


On Friday, 13 December 2013 at 16:37:51 UTC, Gary Willoughby 
wrote:
> I have the following code which is massively simplified from a 
> larger type. The problem occurs between assigning the value to 
> the type and retrieving it.
>
> The value is assigned through opAssign and the assert passes. 
> When using a property to retrieve the same data the assert 
> fails!
>
> import std.bitmanip;
> import std.stdio;
> import std.traits;
>
> struct IpAddress
> {
> 	private ubyte[] _octets;
>
> 	this(uint value)
> 	{
> 		this.opAssign(value);
> 	}
>
> 	public @property ubyte[] data()
> 	{
> 		assert(this._octets == [1, 2, 3, 4]);
> 		return this._octets;
> 	}
>
> 	public void opAssign(uint value)
> 	{
> 		this._octets = value.nativeToBigEndian();
> 		assert(this._octets == [1, 2, 3, 4]);
> 	}
> }
>
> unittest
> {
> 	auto ipAddress = IpAddress(0x01020304);
> 	assert(ipAddress.data == [1, 2, 3, 4]);
> }
>
> Any ideas why?
>
> On a side note i also expected nativeToBigEndian to byte flip 
> the hex literal, no idea why it hasn't, it's been a long day... 
> I'm using MacOSX (Intel).
>
> Compiled with: rdmd --force -de -debug -main -property 
> -unittest -w file.d

opAssign is escaping a reference to its stack by assigning the 
static array to the slice _octets. Therefore, garbage.


More information about the Digitalmars-d-learn mailing list