What Is the alignof Operator in C?
This article explores the alignof operator in the C
programming language, a feature introduced to inspect the memory
boundary and alignment requirements of data types. It covers the
fundamental concept of memory alignment, how alignof was
standardized across modern C specifications, its basic syntax and
practical code examples, and why alignment awareness is essential for
writing high-performance, robust software.
Memory Alignment and the Need for Inspection
Computer hardware accesses memory in multi-byte chunks (typically 4, 8, or more bytes at a time) rather than individual single bytes. To optimize data transfer speeds and satisfy CPU architecture constraints, data types must be placed at memory addresses that are multiples of specific byte counts. For example, a 4-byte integer is typically aligned on an address divisible by 4. Placing an object at an improper boundary can result in degraded performance or, on certain processor architectures, hardware-level alignment faults.
Before modern standards, inspecting these requirements required
platform-dependent compiler extensions or complex pointer arithmetic
macros. The alignof operator provides a standard, portable
way to query these boundary requirements directly at compile time.
Evolution: C11 to C23
The operator was first formally introduced in C11 as a keyword named
_Alignof. To provide cleaner syntax similar to C++, the
header <stdalign.h> was introduced, defining
alignof as a macro expanding to _Alignof.
With the arrival of C23, alignof became a first-class,
reserved keyword in C, removing the strict need to include
<stdalign.h> to use the clean syntax.
Syntax and Return Value
The operator takes a type name as its operand and evaluates to an
integer constant of type size_t.
size_t alignment = alignof(type_name);Unlike sizeof, which accepts both types and runtime
expressions, alignof in standard C strictly requires a
complete object type inside the parentheses. The value returned
represents the minimum number of bytes to which an instance of that type
must be aligned.
Code Example
Below is a practical example showing how alignof queries
alignment requirements for primitive types and composite structures:
#include <stdio.h>
#include <stdalign.h> // Required for C11/C17; optional in C23
struct MixedData {
char a; // 1 byte
double b; // 8 bytes
int c; // 4 bytes
};
int main(void) {
printf("Alignment of char: %zu\n", alignof(char));
printf("Alignment of int: %zu\n", alignof(int));
printf("Alignment of double: %zu\n", alignof(double));
printf("Alignment of struct MixedData: %zu\n", alignof(struct MixedData));
return 0;
}In this example, struct MixedData will typically report
an alignment of 8 bytes on modern 64-bit systems because its largest
member, double, requires 8-byte alignment. The compiler
automatically adds padding to ensure each field, as well as the
structure itself, respects these constraints.
Practical Applications
- Custom Memory Allocators: When building custom slab
or arena allocators, developers must ensure that dynamically reserved
memory buffers satisfy the alignment constraints of the objects stored
inside them.
alignofeliminates guesswork when calculating proper address offsets. - SIMD and Hardware Acceleration: Vector instructions
(such as AVX or NEON) frequently require strict alignment (e.g., 16, 32,
or 64 bytes).
alignofverifies that types meet these target constraints. - Serialization and Binary Layouts: Verifying type boundaries helps identify implicit padding, ensuring consistent memory representations when mapping data over networks or directly into hardware registers.