Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. int x = 1;
  2. char c = *((char*)&x);
  3.  
  4. int x = 1;
  5. char c = *((char*)&x);
  6.  
  7. MyClass x; // object of MyClass
  8. MyClass *x; // pointer to an object of MyClass - the actual value is undefined and trying to access it will most likely result in an access violation (due to reading somewhere random).
  9. MyClass *x = 0; // same as above, but now the default value is defined and you're able to detect whether it's been set (accessing it would essentially be a "null reference exception"; but it's actually a null pointer).
  10. MyClass &x = MyClass(); // creating a new reference pointing to an existing object. This would be Java's "MyClass x = new MyClass();"
  11.  
  12. int x = 1;
  13. char c = (char) x; // Lose precision
  14.  
  15. int x = 1;
  16. char *c = (char *)x;
  17.  
  18. char *c;
  19. c = 1; // Set the address of c to 0x0000000000000001
  20.  
  21. #define PIC_LOC 0x1000
  22. #define PIC_ENABLE_PORT *((char*)(PIC_LOC+0x10))
  23. #define BIT_ENABLE (1 << 3)
  24.  
  25. ...
  26. PIC_ENABLE_PORT |= BIT_ENABLE;
  27. ...
  28.  
  29. // 1a: promote int to double to get the correct type of division
  30.  
  31. int numerator = rand(), denominator = rand();
  32. double d = double(numerator) / double(denominator);
  33.  
  34. // 1b: convert int to double to achieve a particular argument deduction
  35.  
  36. int n;
  37. template <typename T> void do_numeric_stuff(T x) { /* ... */ }
  38.  
  39. do_numeric_stuff(double(n));
  40.  
  41. struct B { }; struct D : B { };
  42. D x;
  43.  
  44. D * p = &x; // pointer to x
  45. B * q = p; // implicit conversion; may change the value!
  46.  
  47. std::ofstream file("output.bin"); // output file
  48. char large_buffer[HUGE]; // in-memory buffer
  49.  
  50. unsigned int n = get_data();
  51.  
  52. char const * p = reinterpret_cast<char const *>(&n);
  53. file.write(p, p + sizeof n); // write the bytes of `n`
  54. std::copy(p, p + sizeof n, large_buffer); // ditto
  55.  
  56. std::copy(large_buffer + 17, large_buffer + 17 + sizeof n,
  57. reinterpret_cast<char *>(&n)); // repopulate `n` from buffer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement