Skip to content

Instantly share code, notes, and snippets.

@hclarke

hclarke/alignmentof

Created Nov 12, 2014
Embed
What would you like to do?
#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
You can’t perform that action at this time.