alignof operator (since C++11) - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# alignof operator (since C++11)

From cppreference.com
< cpp‎ | language

Queries alignment requirements of a type

### [edit] Syntax

`alignof(` type-id `)` |  |

Returns a value of type std::size_t.

### [edit] Explanation

Returns the alignment, in bytes, required for any instance of the type indicated by type-id, which is either complete type, an array type, or a reference type.

If the type is reference type, the operator returns the alignment of referenced type; if the type is array type, alignment requirement of the element type is returned.

The types char, signed char, and unsigned char have the weakest (smallest) alignment supported by the implementation.

### [edit] Keywords

`alignof`

### [edit] Example

Run this code

    #include <iostream>

    struct Empty {};

    struct Foo {
         int f2;
         float f1;
         char c;
    };

    int main()
    {
        std::cout << "alignment of empty class: " << alignof(Empty) << '\n'
                  << "alignment of pointer : "    << alignof(int*)  << '\n'
                  << "alignment of char : "       << alignof(char)  << '\n'
                  << "alignment of Foo : "        << alignof(Foo)   << '\n' ;
    }

Output:

    alignment of empty class: 1
    alignment of pointer : 8
    alignment of char : 1
    alignment of Foo : 4

### [edit] See also

alignment requirement | restricts the addresses at which an object may be allocated
alignas specifier | specifies that the storage for the variable should be aligned by specific amount (C++11) [edit]
alignment_of (C++11) | obtains the type's alignment requirements (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/language/alignof&oldid=67242"