* sheepbao <listome...@gmail.com> [180814 10:53]:
> Thank you for your detailed answer.
> I thought it would crash in this line (b.Z = "zz") of code. But it dose not 
> happen and the program print "zz".
> I don't understand why this program crash in return, not in b.Z = "zz". 
> Because I agree with your opinion,  The field Z of (*b) is beyond the 
> memory that was allocated 
> on the heap or reserved on the stack for a.

It does not crash when assigning to b.Z because b.Z references valid,
writable memory.  It overwrites what some other part of the code wrote
there for a different purpose.  It is not until the incorrect value is
read and used for the original intended purpose that incorrect behavior
can be noticed.  If nothing ever reads from that location afterwards,
than no incorrect behavior can be observed.

It might crash when assigning to b.Z if that memory location is not
writable or is not within the program's address space.  This could
happen, for instance, if the memory allocated for a were at the very end
of the program's address space.  This is extremely unlikely under most
circumstances.

Once you use unsafe to convert a pointer from one type to another, you
become responsible for ensuring that your use is appropriate; the
compiler no longer attempts to keep track of the memory to which the
unsafe pointer points.

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to