Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 2.14 KB

examples-of-structure-alignment.md

File metadata and controls

75 lines (56 loc) · 2.14 KB
title Examples of Structure Alignment | Microsoft Docs
ms.custom
ms.date 03/26/2018
ms.technology
cpp-tools
ms.topic conceptual
dev_langs
C++
helpviewer_keywords
structure alignment
examples [C++], structure alignment
ms.assetid 03d137bf-5cc4-472e-9583-6498f2534199
author corob-msft
ms.author corob
ms.workload
cplusplus

Examples of Structure Alignment

The following four examples each declare an aligned structure or union, and the corresponding figures illustrate the layout of that structure or union in memory. Each column in a figure represents a byte of memory, and the number in the column indicates the displacement of that byte. The name in the second row of each figure corresponds to the name of a variable in the declaration. The shaded columns indicate padding that is required to achieve the specified alignment.

Example 1

// Total size = 2 bytes, alignment = 2 bytes (word).

_declspec(align(2)) struct {
    short a;      // +0; size = 2 bytes
}

AMD conversion example

Example 2

// Total size = 24 bytes, alignment = 8 bytes (quadword).

_declspec(align(8)) struct {
    int a;       // +0; size = 4 bytes
    double b;    // +8; size = 8 bytes
    short c;     // +16; size = 2 bytes
}

AMD conversion example

Example 3

// Total size = 22 bytes, alignment = 4 bytes (doubleword).

_declspec(align(4)) struct {
    char a;       // +0; size = 1 byte
    short b;      // +2; size = 2 bytes
    char c;       // +4; size = 1 byte
    int d;        // +8; size = 4 bytes
}

AMD conversion example

Example 4

// Total size = 8 bytes, alignment = 8 bytes (quadword).

_declspec(align(8)) union {
    char *p;      // +0; size = 8 bytes
    short s;      // +0; size = 2 bytes
    long l;       // +0; size = 4 bytes
}

AMD conversion example

See also

Types and Storage