fork(1) download
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class String
  6. {
  7. private:
  8. char *s;
  9. int size;
  10. public:
  11.  
  12. String(const char *str = NULL); // constructor
  13. // String(const String&);
  14. ~String() { delete [] s; }// destructor
  15. void print() { cout << s << endl; }
  16. void change(const char *); // Function to change
  17. };
  18.  
  19. String::String(const char *str)
  20. {
  21. size = strlen(str);
  22. s = new char[size+1];
  23. strcpy(s, str);
  24. }
  25.  
  26. void String::change(const char *str)
  27. {
  28. delete [] s;
  29. size = strlen(str);
  30. s = new char[size+1];
  31. strcpy(s, str);
  32. }
  33.  
  34. /* String::String(const String& old_str)
  35. {
  36.   size = old_str.size;
  37.   s = new char[size+1];
  38.   strcpy(s, old_str.s);
  39. }*/
  40. int main()
  41. {
  42. String str1("GeeksQuiz");
  43. String str2 = str1;
  44.  
  45. str1.print(); // what is printed ?
  46. str2.print();
  47.  
  48. str2.change("GeeksforGeeks");
  49.  
  50. str1.print(); // what is printed now ?
  51. // str2.print();
  52. return 0;
  53. }
Runtime error #stdin #stdout #stderr 0s 3276KB
stdin
Standard input is empty
stdout
GeeksQuiz
GeeksQuiz

stderr
*** Error in `./prog': double free or corruption (fasttop): 0x0941d008 ***