Site Tools


wiki:h-stdalign

Table of Contents

<stdalign.h>

<stdalign.h> provides alignas() and alignof() (C11) for memory alignment control. alignof(type) returns its alignment requirement; alignas(N) forces a variable or struct member to align to N bytes.

Use it for SIMD operations, hardware register access, or matching binary layouts.

Example

This example demonstrates alignment queries and enforces 16-byte alignment.

// compile: gcc -std=c11 -o alignexample alignexample.c
// run: ./alignexample
// description: query and enforce memory alignment
 
#include <stdalign.h>
#include <stdint.h>
#include <stdio.h>
 
int main() {
    printf("alignof(char) = %zu\n", alignof(char));
    printf("alignof(int) = %zu\n", alignof(int));
    printf("alignof(double) = %zu\n", alignof(double));
 
    alignas(16) float vec[4];
    printf("vec[0] address mod 16 = %zu (should be 0)\n", 
           (uintptr_t)vec % 16);
 
    return 0;
}
wiki/h-stdalign.md · Last modified: by 127.0.0.1