Search This Blog

Thursday, January 6, 2011

The operator : __alignof( )

 http://msdn.microsoft.com/en-us/library/45t0s5f4%28VS.71%29.aspx

__alignof Operator

Visual Studio .NET 2003

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

__alignof( type )
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:
END Microsoft Specific

See Also

Expressions with Unary Operators | C++ Keywords