Dela via


__alignof Operator

Microsoft Specific

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

__alignof( 
   type  
)

Remarks

For example:

Expression

Value

__alignof( char )

1

__alignof( short )

2

__alignof( int )

4

__alignof( __int64 )

8

__alignof( float )

4

__alignof( double )

8

__alignof( char* )

4

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:

See Also

Reference

Expressions with Unary Operators

C++ Keywords