Confused about referencing in D
Gorge Jingale via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 13 23:23:15 PDT 2016
I'm pretty confused why the following code doesn't work as
expected.
GetValue is a struct.
auto value = GetValue();
memcpy(&value, &val, Val.sizeof);
But if I plug in value direct to memset it works!!
memcpy(&GetValue(), &val, Val.sizeof);
GetValue returns memory to stick a value in and the memcpy is
copying it over. It is very confusing logic. Should I not expect
the two cases two be identical?
I think that the assignment in the first case is not assigning
the reference returned by auto ref GetValue().
I am expecting it to work like a pointer,
void* value = &GetValue();
memcpy(value, &val, Val.sizeof);
so to speak but it works more like
S value;
value = GetValue();
memcpy(&value, &val, Val.sizeof);
which copies the data to the memory created for value and not the
one returned by GetValue.
More information about the Digitalmars-d-learn
mailing list