fork download
  1. #include <stdio.h>
  2. #include <stddef.h>
  3.  
  4. /*
  5. ** Attempt to find any holes in struct toCheckSize.
  6. **
  7. ** begin struct toCheckSize
  8. ** ----
  9. ** (possible hole above a)
  10. ** ----
  11. ** int a;
  12. ** ----
  13. ** (possible hole below a)
  14. ** ----
  15. ** char b;
  16. ** ----
  17. ** (possible hole below b)
  18. ** ----
  19. ** end struct
  20. */
  21.  
  22. struct toCheckSize
  23. {
  24. int a;
  25. char b;
  26. } x;
  27.  
  28. #define XXX \
  29.   X(struct_size, sizeof(x)) \
  30.   X(a_size, sizeof(x.a)) \
  31.   X(b_size, sizeof(x.b)) \
  32.   X(a_offset, offsetof(struct toCheckSize, a)) \
  33.   X(b_offset, offsetof(struct toCheckSize, b)) \
  34.   X(compact_size, a_size + b_size) \
  35.   X(hole_size, struct_size - compact_size) \
  36.   X(hole_size_above_a, a_offset) \
  37.   X(hole_size_below_a, b_offset - (a_offset + a_size)) \
  38.   X(hole_size_below_b, struct_size - (b_offset + b_size))
  39.  
  40. int main(void) {
  41. #define X(N, V) size_t N = V;
  42. XXX
  43. #undef X
  44. #define X(N, V) printf("%s = %zu\n", #N, N);
  45. XXX
  46. #undef X
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
struct_size = 8
a_size = 4
b_size = 1
a_offset = 0
b_offset = 4
compact_size = 5
hole_size = 3
hole_size_above_a = 0
hole_size_below_a = 0
hole_size_below_b = 3