code_junkie

What is the strict aliasing rule?

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. struct Msg
  2. {
  3. unsigned int a;
  4. unsigned int b;
  5. };
  6.  
  7. int main()
  8. {
  9. // Get a 32-bit buffer from the system
  10. uint32_t* buff = malloc(sizeof(Msg));
  11.  
  12. // Alias that buffer through message
  13. Msg* msg = (Msg*)(buff);
  14.  
  15. // Send a bunch of messages
  16. for (int i =0; i < 10; ++i)
  17. {
  18. msg->a = i;
  19. msg->b = i+1;
  20. SendWord(buff[0] );
  21. SendWord(buff[1] );
  22.  
  23. }
  24.  
  25. }
  26.  
  27. void SendMessage(uint32_t* buff, size_t size32)
  28. {
  29. for (int i = 0; i < size32; ++i)
  30. {
  31. SendWord(buff[i]);
  32. }
  33. }
  34.  
  35. for (int i =0; i < 10; ++i)
  36. {
  37. msg->a = i;
  38. msg->b = i+1;
  39. SendMessage(buff, 2);
  40. }
  41.  
  42. char*
Add Comment
Please, Sign In to add comment