On Tuesday, 5 November 2019 at 10:32:03 UTC, Ferhat Kurtulmuş
wrote:
On Tuesday, 5 November 2019 at 10:31:05 UTC, Ferhat Kurtulmuş
wrote:
On Tuesday, 5 November 2019 at 10:13:59 UTC, Mike Parker wrote:
[...]
Yep, it is obvious that my code is wrong. s1 and s2 point to
the same memory address. I could obtain my desired behavior
with copy constructor. The documentation also say "WARNING:
The postblit is considered legacy and is not recommended for
new code. Code should use copy constructors defined in the
previous section".
I meant the example as an answer to your statement, "I wonder how
new memory is allocated without an explicit malloc here". The
postblit was intended as a chance to "fixup" everything when you
needed a deep copy. The new struct is initialized as a shallow
copy, so when you enter into the postblit, the pointer is already
pointing at the original location. Without assigning it a new
malloc'ed address, your memcpy was essentially overwriting the
original location with its own data.