Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. typedef struct AlignedByte_t {
  6.     uint8_t     b;  /* Byte, aligned at 4-bytes border. */
  7.     uint32_t    align;  /* Unused, forces structure to be aligned. */
  8. } AlignedByte_t;
  9.  
  10. int
  11. main(void)
  12. {
  13.     /*
  14.      * Unused, allocated to obtain proper addresses.
  15.      */
  16.     uint8_t b;
  17.     AlignedByte_t align_byte;
  18.  
  19.     /*
  20.      * Pointers casting.
  21.      *
  22.      * Uncomment the aligned byte address taking to see how output of the
  23.      * program will be changed.
  24.      */
  25.     uint8_t *var = &b; // [1]
  26. //  uint8_t *var = &align_byte.b; // [2]
  27.     int *pVar = (void *) var; // [3]
  28.  
  29.     /* Print values to be checked manually. */
  30.     printf("pVar=0x%lx, sizeof(pVar)=%lu\n",
  31.            ((unsigned long) pVar), sizeof(pVar));
  32.  
  33.     /* Check "pVar" alignment. */
  34.     if ((((unsigned long) pVar) % sizeof(pVar)) != 0) {
  35.         printf("\'pVar\' isn't properly aligned!\n");
  36.     }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement