fork download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. struct node {
  8. std::string data;
  9. ~node() { cout<<"object destroyed"<<endl; }
  10. };
  11.  
  12. int main() {
  13. node * n = new node;
  14. void *p = malloc(sizeof(node)); // not so a good idea !
  15. node *n2 = new (p)node; // but ok, it's feasible.
  16.  
  17. n->data.assign("string");
  18. n2->data.assign("string");
  19. cout << p<<" is " << n2 <<endl;
  20. delete n;
  21. delete n2;
  22. free(p);
  23.  
  24. return 0;
  25. }
Runtime error #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
0x86a8a20 is 0x86a8a20
object destroyed
object destroyed
stderr
*** Error in `./prog': double free or corruption (fasttop): 0x086a8a20 ***