max_align_t

来自cppreference.com
< c‎ | types
在标头 <stddef.h> 定义
typedef /* 由实现定义 */ max_align_t;
(C11 起)

max_align_t 是对齐要求至少和其他任何一种标量类型一样严格(一样大)的类型。

注解

malloc 这样的分配函数所返回的指针是为任意对象对齐的,这表示它们至少和 max_align_t 一样严格。

示例

#include <inttypes.h>
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    size_t a = alignof(max_align_t);
    printf("max_align_t 的对齐是 %zu (%#zx)\n", a, a);
 
    void *p = malloc(123);
    printf("从 malloc(123) 获得的地址为 %#" PRIxPTR"\n",
            (uintptr_t)p);
    free(p);
}

可能的输出:

max_align_t 的对齐是 16 (0x10)
从 malloc(123) 获得的地址为 0x1fa67010

引用

  • C17 标准(ISO/IEC 9899:2018):
  • 7.19 Common definitions <stddef.h> (第 211 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.19 Common definitions <stddef.h> (第 288 页)

参阅