Keith_S_Thompson

Example for https://stackoverflow.com/q/47418541/827263

Nov 21st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstddef>
  3. int main() {
  4. struct A {
  5. char c;
  6. short s;
  7. char c2;
  8. int i;
  9. };
  10. A obj;
  11. std::cout << "sizeof (A) = " << sizeof (A) << "\n";
  12. std::cout << " char c at offset " << offsetof(A, c) << " size " << sizeof obj.c << "\n";
  13. std::cout << " short s at offset " << offsetof(A, s) << " size " << sizeof obj.s << "\n";
  14. std::cout << " char c2 at offset " << offsetof(A, c2) << " size " << sizeof obj.c2 << "\n";
  15. std::cout << " int i at offset " << offsetof(A, i) << " size " << sizeof obj.i << "\n";
  16. }
  17.  
  18. // Output on my system (g++, Linux x86_64):
  19. //
  20. // sizeof (A) = 12
  21. // char c at offset 0 size 1
  22. // short s at offset 2 size 2
  23. // char c2 at offset 4 size 1
  24. // int i at offset 8 size 4
Add Comment
Please, Sign In to add comment