fork download
  1. #include<memory>
  2. #include<cstring>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. std::unique_ptr<std::uint64_t[]> block(new std::uint64_t[100]);
  9.  
  10. // Let's assume sizeof(float) == 4 and sizeof(double) == 8. I want to store a float and a double in block and print the value.
  11.  
  12. float* pf = reinterpret_cast<float*>(&block[0]);
  13. double* pd = reinterpret_cast<double*>(&block[1]);
  14.  
  15. memmove(pf, &block[0], 8);
  16. *pf = 1.1;
  17. memmove(&block[0], pf, 8);
  18.  
  19. memmove(pd, &block[0], 8);
  20. *pd = 2.2;
  21. memmove(&block[0], pd, 8);
  22.  
  23. memmove(pf, &block[0], 8);
  24. std::cout << *pf << std::endl;
  25. memmove(&block[0], pf, 8);
  26.  
  27. memmove(pd, &block[0], 8);
  28. std::cout << *pd << std::endl;
  29. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
1.1
2.2