Skip to content

Instantly share code, notes, and snippets.

@hclarke
Created November 12, 2014 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hclarke/8fcca92a5d6cbc698143 to your computer and use it in GitHub Desktop.
Save hclarke/8fcca92a5d6cbc698143 to your computer and use it in GitHub Desktop.
#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]);
}
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