fork download
  1.  
  2. #include <iostream>
  3. #include <string.h>
  4. using namespace std;
  5. char* strcat_m(const char *str_dest,const char *str_src) {
  6. static char *new_str = nullptr;
  7. if (new_str){
  8. printf("~~del: %d \n",size_t(new_str));
  9. delete[] new_str;
  10. }
  11.  
  12. new_str= new char[strlen(str_dest)+strlen(str_src)+1];
  13. printf("~~new: %d \n",size_t(new_str));
  14. strcpy(new_str,"");
  15. strcat(new_str,str_dest);
  16. strcat(new_str,str_src);
  17. return new_str;
  18. };
  19.  
  20. int main()
  21. {
  22. printf(strcat_m("123"," 213123\n"));
  23. printf(strcat_m("A "," B\n"));
  24. const char* rew=strcat_m("asd"," asd\n");
  25. //Но если написать delete[] rew, то программа вылетает при следующем вызове strcat_m
  26. rew=strcat_m("+++ ","--- \n");
  27. printf(rew);
  28. }
  29.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
~~new: 28822560 
123  213123
~~del: 28822560 
~~new: 28822560 
A    B
~~del: 28822560 
~~new: 28822560 
~~del: 28822560 
~~new: 28822560 
+++ ---