fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. struct myobject {
  8. int x;
  9. double y;
  10. std::string z;
  11. };
  12.  
  13. void printUC(unsigned char* data, size_t len){
  14. for (int i = 0; i < len; i++) {
  15. printf("%x", data[i]);
  16. }
  17. printf("\n");
  18. }
  19.  
  20. void printObj(const myobject& data){
  21. std::cout << data.x << " " << data.y << " " << data.z << '\n';
  22. }
  23.  
  24. int main() {
  25. // your code goes here
  26. myobject obj1 = {
  27. 42, 42.42, "hello"
  28. };
  29. unsigned char* bytes1 = new unsigned char[sizeof(obj1)];
  30. memcpy(bytes1, static_cast<void *>(&obj1), sizeof(obj1));
  31. myobject obj2 = {
  32. 42, 42.42, "hello"
  33. };
  34. unsigned char* bytes2 = new unsigned char[sizeof(obj2)];
  35. memcpy(bytes2, static_cast<void*>(&obj2), sizeof(obj2));
  36.  
  37. printUC(bytes1, sizeof(obj1));
  38. printUC(bytes2, sizeof(obj2));
  39.  
  40. std::cout << memcmp(bytes1, bytes2, sizeof(obj1)) << '\n';
  41.  
  42. auto tmp = myobject{};
  43. /* memcpy(&tmp, bytes1, sizeof(obj1));
  44. printObj(tmp);
  45. memcpy(&tmp, bytes2, sizeof(obj2));
  46. printObj(tmp);
  47. */
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
2a0000000f6285c8fc2354540f032d05cff7f005000000068656c6c6f00000000000
2a0000000f6285c8fc23545402033d05cff7f005000000068656c6c6f00000000000
208