Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstddef>
- int main() {
- struct A {
- char c;
- short s;
- char c2;
- int i;
- };
- A obj;
- std::cout << "sizeof (A) = " << sizeof (A) << "\n";
- std::cout << " char c at offset " << offsetof(A, c) << " size " << sizeof obj.c << "\n";
- std::cout << " short s at offset " << offsetof(A, s) << " size " << sizeof obj.s << "\n";
- std::cout << " char c2 at offset " << offsetof(A, c2) << " size " << sizeof obj.c2 << "\n";
- std::cout << " int i at offset " << offsetof(A, i) << " size " << sizeof obj.i << "\n";
- }
- // Output on my system (g++, Linux x86_64):
- //
- // sizeof (A) = 12
- // char c at offset 0 size 1
- // short s at offset 2 size 2
- // char c2 at offset 4 size 1
- // int i at offset 8 size 4
Add Comment
Please, Sign In to add comment