fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <cstring>
  4.  
  5. struct A
  6. {
  7. std::unique_ptr<int> up_myInt;
  8. A(int val)
  9. :
  10. up_myInt(std::make_unique<int>(val))
  11. {}
  12. };
  13.  
  14. int main()
  15. {
  16. A a(1);
  17. {
  18. A b(2);
  19. memcpy(&a, &b, sizeof(A));
  20. std::cout << *a.up_myInt << std::endl;
  21. //b gets deleted, and the memory b.up_myInt points to is gone
  22. }
  23. std::cout << *a.up_myInt << std::endl;
  24. return 0;
  25. }
Runtime error #stdin #stdout #stderr 0s 3472KB
stdin
Standard input is empty
stdout
2
0
stderr
*** Error in `./prog': double free or corruption (fasttop): 0x08433a20 ***