fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstddef>
  6.  
  7. int main() {
  8. //First string
  9. std::string src = "This is a test";
  10.  
  11. //Allocate space for second string
  12. void* destalloc = std::malloc(sizeof(std::string));
  13. if(destalloc == NULL) {
  14. std::cerr << "Could not allocate memory" << std::endl;
  15. return 1;
  16. }
  17. std::string* dest = static_cast<std::string*>(destalloc);
  18.  
  19. //Copy first string to second string
  20. std::memcpy(dest, &src, sizeof(std::string));
  21.  
  22. //Modify second string
  23. (*dest)[0] = 't';
  24.  
  25. //Output both strings
  26. std::cout << src << std::endl;
  27. std::cout << (*dest) << std::endl;
  28.  
  29. //Free memory for second string
  30. std::free(destalloc);
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
this is a test
this is a test