Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointers stopped working correctly #116

Open
Yaisis opened this issue Dec 31, 2019 · 0 comments
Open

pointers stopped working correctly #116

Yaisis opened this issue Dec 31, 2019 · 0 comments

Comments

@Yaisis
Copy link

Yaisis commented Dec 31, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant