gcc 过度对齐的新支持 (alignas)
gcc over-aligned new support (alignas )
我很难找到有关 GCC 的 aligned-new 警告和 gcc -faligned-new 选项的更多信息。在 gcc 7.2.0 上编译(没有 --std=c++17)并尝试定义对齐结构,例如:
struct alignas(64) Foo { int x; }
只是做一个普通人:
Foo * f = new Foo();
给我以下警告和建议:
alignas.cpp:36:25: warning: ‘new’ of type ‘Foo’ with extended alignment 64 [-Waligned-new=]
Foo * f = new Foo();
^
alignas.cpp:36:25: note: uses ‘void* operator new(long unsigned int)’, which does not have an alignment parameter
alignas.cpp:36:25: note: use ‘-faligned-new’ to enable C++17 over-aligned new support
我知道默认情况下 new
只会 return 内存对齐 alignof( std::max_align_t )
(对我来说是 16),但我不清楚的是,如果我通过-faligned-new,gcc 现在会代表我执行 new
的正确新对齐吗?
不幸的是,关于这个的 gcc 文档非常缺乏。
来自 gcc's manual:
-faligned-new
Enable support for C++17 new
of types that require more alignment than void* ::operator new(std::size_t)
provides. A numeric argument such as -faligned-new=32 can be used to specify how much alignment (in bytes) is provided by that function, but few users will need to override the default of alignof(std::max_align_t)
.
这意味着 -faligned-new 只是使 P0035R4 中添加的 aligned-new 功能可用,而无需完全启用 C++17 支持。
C++ 标准中的相关位:
来自 [cpp.predefined]:
__STDCPP_DEFAULT_NEW_ALIGNMENT__
An integer literal of type std::size_t
whose value is the alignment guaranteed by a call to operator new(std::size_t)
or operator new[](std::size_t)
. [ Note: Larger alignments will be passed to operator new(std::size_t, std::align_val_t)
, etc. (8.3.4). — end note ]
来自[基本.align/3]:
A new-extended alignment is represented by an alignment greater than __STDCPP_DEFAULT_NEW_ALIGNMENT__
并且来自[expr.new/14]:
Overload resolution is performed on a function call created by assembling an argument list. The first argument is the amount of space requested, and has type std::size_t
. If the type of the allocated object has new-extended alignment, the next argument is the type’s alignment, and has type std::align_val_t
.
因此在您使用 C++17 或 -faligned-new 的情况下,因为 Foo
具有 new-extended 对齐方式 ,Foo* f = new Foo();
将调用 void* operator new(size_t, align_val_t)
分配内存和 return 指向在 64 字节边界上正确对齐的 Foo
对象的指针。根据早期标准,情况并非如此。
我很难找到有关 GCC 的 aligned-new 警告和 gcc -faligned-new 选项的更多信息。在 gcc 7.2.0 上编译(没有 --std=c++17)并尝试定义对齐结构,例如:
struct alignas(64) Foo { int x; }
只是做一个普通人:
Foo * f = new Foo();
给我以下警告和建议:
alignas.cpp:36:25: warning: ‘new’ of type ‘Foo’ with extended alignment 64 [-Waligned-new=]
Foo * f = new Foo();
^
alignas.cpp:36:25: note: uses ‘void* operator new(long unsigned int)’, which does not have an alignment parameter
alignas.cpp:36:25: note: use ‘-faligned-new’ to enable C++17 over-aligned new support
我知道默认情况下 new
只会 return 内存对齐 alignof( std::max_align_t )
(对我来说是 16),但我不清楚的是,如果我通过-faligned-new,gcc 现在会代表我执行 new
的正确新对齐吗?
不幸的是,关于这个的 gcc 文档非常缺乏。
来自 gcc's manual:
-faligned-new
Enable support for C++17new
of types that require more alignment thanvoid* ::operator new(std::size_t)
provides. A numeric argument such as -faligned-new=32 can be used to specify how much alignment (in bytes) is provided by that function, but few users will need to override the default ofalignof(std::max_align_t)
.
这意味着 -faligned-new 只是使 P0035R4 中添加的 aligned-new 功能可用,而无需完全启用 C++17 支持。
C++ 标准中的相关位:
来自 [cpp.predefined]:
__STDCPP_DEFAULT_NEW_ALIGNMENT__
An integer literal of typestd::size_t
whose value is the alignment guaranteed by a call tooperator new(std::size_t)
oroperator new[](std::size_t)
. [ Note: Larger alignments will be passed tooperator new(std::size_t, std::align_val_t)
, etc. (8.3.4). — end note ]
来自[基本.align/3]:
A new-extended alignment is represented by an alignment greater than
__STDCPP_DEFAULT_NEW_ALIGNMENT__
并且来自[expr.new/14]:
Overload resolution is performed on a function call created by assembling an argument list. The first argument is the amount of space requested, and has type
std::size_t
. If the type of the allocated object has new-extended alignment, the next argument is the type’s alignment, and has typestd::align_val_t
.
因此在您使用 C++17 或 -faligned-new 的情况下,因为 Foo
具有 new-extended 对齐方式 ,Foo* f = new Foo();
将调用 void* operator new(size_t, align_val_t)
分配内存和 return 指向在 64 字节边界上正确对齐的 Foo
对象的指针。根据早期标准,情况并非如此。