Skip to content

pointers stopped working correctly #116

Description

@Yaisis

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions