Advertisement
alaestor

arirthmetic and aliasing

Dec 26th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstddef>
  3.  
  4. void f1()
  5. {
  6.     std::cout << "byte[] and byte* arithmetic" << std::endl;
  7.  
  8.     std::byte x[5];
  9.     std::byte* ptr = x;
  10.  
  11.     for (int c = 0; c < 5; c++)
  12.         std::cout << &x[c] << ",";
  13.  
  14.     std::cout << " <- bytes" << std::endl;
  15.  
  16.     for (int c = 0; c < 5; c++)
  17.         std::cout << ptr+c << ",";
  18.  
  19.     std::cout << " <- bytes" << std::endl << std::endl;
  20. }
  21.  
  22. void f2()
  23. {
  24.     std::cout << "int[] and byte* arithmetic (aliasing)" << std::endl;
  25.  
  26.     int x[5];
  27.     std::byte* ptr = reinterpret_cast<std::byte*>(x);
  28.  
  29.     for (int c = 0; c < 5; c++)
  30.         std::cout << &x[c] << ",";
  31.  
  32.     std::cout << " <- ints" << std::endl;
  33.  
  34.     for (int c = 0; c < 5; c++)
  35.         std::cout << ptr+c << ",";
  36.  
  37.     std::cout << " <- bytes" << std::endl << std::endl;
  38. }
  39.  
  40. int main()
  41. {
  42.     f1();
  43.     f2();
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement