fork(3) download
  1. #include <iostream>
  2. #include <cstdint>
  3.  
  4. struct Three {
  5. char* a = "hello world!";
  6. char* b = "hesoyam!";
  7. char* c = "showmethemoney!";
  8. };
  9.  
  10. int main() {
  11. int size = 3;
  12. auto c = (Three*)std::malloc(size * sizeof(Three*));
  13.  
  14. for(int i = 0;i < size; ++i) {
  15. c[i] = Three();
  16. }
  17.  
  18. auto lastElementAddress1 = reinterpret_cast<uintptr_t>(c + (size - 1));
  19. auto lastElementAddress2 = reinterpret_cast<uintptr_t>(c + (size - 1) * sizeof(Three));
  20.  
  21. auto lastElement1 = reinterpret_cast<Three*>(lastElementAddress1);
  22. auto lastElement2 = reinterpret_cast<Three*>(lastElementAddress2);
  23. std::cout << "v1: " << lastElement1->a << std::endl;
  24. std::cout << "v2: " << lastElement2->a << std::endl;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 4504KB
stdin
Standard input is empty
stdout
v1: hello world!
v2: