__alignof Operator
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

__alignof Operator

Microsoft Specific

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


For example:

Expression Value

__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:

END Microsoft Specific

© 2016 Microsoft