C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - std::addressof
 - std::allocator_traits
 - std::default_delete
 - std::allocator_arg_t
 - std::allocator_arg
 - std::weak_ptr
 - std::enable_shared_from_this
 - std::bad_weak_ptr
 - std::unique_ptr
 - std::scoped_allocator_adaptor
 - std::auto_ptr
 - std::destroy_at
 - std::destroy
 - std::destroy_n
 - std::uninitialized_move
 - std::uninitialized_value_construct
 - std::owner_less
 - std::shared_ptr
 - std::to_address
 - std::assume_aligned
 - std::make_obj_using_allocator
 - C 内存管理库
 - 低层内存管理
 - std::pmr::memory_resource
 - std::allocator
 - std::pointer_traits
 - std::uses_allocator
 - std::uses_allocator_construction_args
 - std::uninitialized_construct_using_allocator
 - std::pmr::polymorphic_allocator
 - std::pmr::get_default_resource
 - std::pmr::set_default_resource
 - std::pmr::new_delete_resource
 - std::pmr::null_memory_resource
 - std::pmr::synchronized_pool_resource
 - std::pmr::unsynchronized_pool_resource
 - std::pmr::monotonic_buffer_resource
 - std::pmr::pool_options
 - std::raw_storage_iterator
 - std::get_temporary_buffer
 - std::return_temporary_buffer
 - std::uninitialized_copy
 - std::uninitialized_fill
 - std::uninitialized_default_construct
 - std::uninitialized_copy_n
 - std::uninitialized_fill_n
 - std::uninitialized_move_n
 - std::uninitialized_default_construct_n
 - std::uninitialized_value_construct_n
 - std::construct_at
 - std::align
 - 注释
 - 日期和时间工具
 - 字符串库
 - 容器库
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - 数值库
 - 输入/输出库
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
std::align
|   定义于头文件  <memory>
  | 
||
|   void* align( std::size_t alignment,              std::size_t size,  | 
(C++11 起) | |
给定指针 ptr 指定大小为 space 的缓冲区,返回按指定 alignment 为 size 字节数对齐的指针,并减小 space 参数对齐所用的字节数。返回首个对齐的地址。
仅以给定对齐量对齐入缓冲区的所需字节数合适,函数才会修改指针。若缓冲区太小,则函数不做任何事并返回 nullptr 。
若 alignment 不是二的幂,则行为未定义。
参数
| alignment | - | 欲求的对齐量 | 
| size | - | 要被对齐的存储的大小 | 
| ptr | - |   指向至少有 space 字节的连续存储的指针
 | 
| space | - | 要在其中操作的缓冲区的大小 | 
返回值
ptr 的调整值,或若提供空间太小则为空指针值。
示例
演示使用 std::align 在内存中放置不同类型的对象
运行此代码
#include <iostream> #include <memory> template <std::size_t N> struct MyAllocator { char data[N]; void* p; std::size_t sz; MyAllocator() : p(data), sz(N) {} template <typename T> T* aligned_alloc(std::size_t a = alignof(T)) { if (std::align(a, sizeof(T), p, sz)) { T* result = reinterpret_cast<T*>(p); p = (char*)p + sizeof(T); sz -= sizeof(T); return result; } return nullptr; } }; int main() { MyAllocator<64> a; // 分配一个 char char* p1 = a.aligned_alloc<char>(); if (p1) *p1 = 'a'; std::cout << "allocated a char at " << (void*)p1 << '\n'; // 分配一个 int int* p2 = a.aligned_alloc<int>(); if (p2) *p2 = 1; std::cout << "allocated an int at " << (void*)p2 << '\n'; // 分配一个 int ,对齐于 32 字节边界 int* p3 = a.aligned_alloc<int>(32); if (p3) *p3 = 2; std::cout << "allocated an int at " << (void*)p3 << " (32 byte alignment)\n"; }
可能的输出:
allocated a char at 0x2ff21a08 allocated an int at 0x2ff21a0c allocated an int at 0x2ff21a20 (32 byte alignment)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 2377 | C++11 |  要求 alignment 为基础或受支持的扩展对齐值
 | 
仅需要为二的幂 | 
参阅
   alignof 运算符
 | 
查询类型的对齐要求 (C++11 起) | 
   alignas 说明符
 | 
指定该变量的存储应该按指定量对齐 (C++11) | 
|    (C++11)  | 
   定义适于用作给定大小的类型的未初始化存储的类型   (类模板)  |