Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

__alignof Operator

C++11 introduces the alignof operator that returns the alignment, in bytes, of the specified type. For maximum portability, you should use the alignof operator instead of the Microsoft-specific __alignof operator.

Microsoft Specific

Returns a value of type size_t that is the alignment requirement of the type.


For example:



__alignof( char )


__alignof( short )


__alignof( int )


__alignof( __int64 )


__alignof( float )


__alignof( double )


__alignof( char* )


The __alignof value is the same as the value for sizeof for basic types. Consider, however, this example:

typedef struct { int a; double b; } S;
// __alignof(S) == 8

In this case, the __alignof value is the alignment requirement of the largest element in the structure.

Similarly, for

typedef __declspec(align(32)) struct { int a; } S;

__alignof(S) is equal to 32.

One use for __alignof would be as a parameter to one of your own memory-allocation routines. For example, given the following defined structure S, you could call a memory-allocation routine named aligned_malloc to allocate memory on a particular alignment boundary.

typedef __declspec(align(32)) struct { int a; double b; } S;
int n = 50; // array size
S* p = (S*)aligned_malloc(n * sizeof(S), __alignof(S));

For more information on modifying alignment, see:

For more information on differences in alignment in code for x86 and x64, see:

© 2015 Microsoft