Compartilhar via


align (Windows CE 5.0)

Send Feedback

The __declspec(align(#)) extended storage-class modifier controls the alignment of global data.

__declspec(align(#))declarator

By aligning frequently used data to the cache line size of a specific processor, you improve cache performance. For example, if you define a structure whose size is less than 32 bytes, you might want to align it to 32 bytes to ensure that objects of that structure type are efficiently cached.

# is the alignment value. Valid entries are powers of two from 1 to 8192 (bytes); for example, 2, 4, 8, 16, 32, or 64.

The declarator is the global data you are declaring as aligned.

You can use __declspec(align( # )) when you define a struct, union, or class, or when you declare a variable.

Without __declspec(align( # )), Visual C++ aligns data on natural boundaries based on the size of the data; for example, 4-byte integers on 4-byte boundaries and 8-byte doubles on 8-byte boundaries.

Data in classes or structures is aligned within the class or structure at the minimum of its natural alignment and the current packing setting (from #pragma pack or the /Zp - Pack Structure Members compiler option). You cannot specify alignment for stack variables. For example:

__declspec(align(32)) struct Str1
{
  int a, b, c, d, e;
};

This type now has a 32-byte alignment attribute, meaning that all instances must start on a 32-byte boundary.

sizeof(struct Str1) is equal to 32 to support creation of aligned array members using code like malloc(n * sizeof(struct Str1)).

Additional structure types declared with this type as a member preserve this type's alignment attribute. That is, any structure with Str1 as an element will have an alignment attribute of at least 32.

The sizeof value for a structure is the offset of the final member plus the member size, rounded up to the nearest multiple of the largest member alignment value or the whole structure alignment value, whichever is greater.

See Also

__declspec

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.