Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the cal
Your object was copied field by field, so inside SomeFunc
you have two instances of Sample
- s1
and x
(but only x
is accessible), and value of x.ptr
is equal to s1.ptr
. Then when SomeFunc
ends, destructor is called and from that point s1.ptr
points to unallocated memory. Its called "dangling pointer".