msgpack-d icon indicating copy to clipboard operation
msgpack-d copied to clipboard

pointers stopped working correctly

Open Yaisis opened this issue 5 years ago • 0 comments

import std.file; import std.stdio; import msgpack;

struct S { byte[] a; byte[] b; byte[] c; }

void main() { S s1; s1.a = [1,2,3,4,5]; s1.b = s1.a; s1.c = s1.a[1..3];

// serialize data
ubyte[] inData = pack(s1);

// write data to a file
std.file.write("file.dat", inData);

// read data from a file
ubyte[] outData = cast(ubyte[])read("file.dat");

// unserialize the data
S s2 = outData.unpack!S();



writeln(s1);	// S([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [2, 3])		// true
writeln(s2);  // S([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [2, 3])		// true

s1.c[1] = 100;
s2.c[1] = 100;

writeln(s1);  // S([1, 2, 100, 4, 5], [1, 2, 100, 4, 5], [2, 100])		//  true
writeln(s2);  // S([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [2, 100])			//  false

assert(s1.a.ptr == s1.b.ptr);	// true
assert(s2.a.ptr == s2.b.ptr); // false

}

I did not notice such problems before, but recently I returned to my project and noticed that it stopped working. Found out that this is because of pointers, as in the code example above.

Yaisis avatar Dec 31 '19 01:12 Yaisis