Created
November 12, 2014 19:14
-
-
Save hclarke/8fcca92a5d6cbc698143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
template<typename T> | |
size_t __alignmentof() { | |
struct padded { char padding; T value; }; | |
return (size_t)(&(((padded*)0)->value)); //offsetof | |
} | |
#define alignmentof(T) __alignmentof<T>() | |
#define p(T) printf("for %s: size=%d, alignment=%d\n", #T, (int)sizeof(T), (int)alignmentof(T)) | |
int main(int argc, char** argv) { | |
p(char); | |
p(short); | |
p(int); | |
p(long long); | |
p(float); | |
p(int[3]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for char: size=1, alignment=1 | |
for short: size=2, alignment=2 | |
for int: size=4, alignment=4 | |
for long long: size=8, alignment=8 | |
for float: size=4, alignment=4 | |
for int[3]: size=12, alignment=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment