海湾合作委员会中 -faligned-new 的值

Value of -faligned-new in GCC

本文关键字:-faligned-new 的值 委员会      更新时间:2023-10-16

我遇到了与此类似的问题 GCC 过度对齐的新支持 (alignas( 所以我添加了-faligned-new编译器标志。

这修复了编译器警告。但是,当编译器使用--verbose标志运行时,我得到此输出 https://pastebin.com/X2QZAtSb,其中最重要的行是

COLLECT_GCC_OPTIONS='-o' 'main' '-faligned-new=1' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'

海湾合作委员会手册(https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect-Options(指出:

-对齐-新

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=1是什么意思?如果我理解正确,它应该等于 8 或 16 或其他什么(默认值为alignof(std::max_align_t)https://en.cppreference.com/w/cpp/types/max_align_t(,而不是 1。

要重现编译简单的main.cpp:

#include <iostream>
int main() {
std::cout << "hello worldn";
return 0;
}

使用g++ main.cpp -o main -faligned-new --verbose

我正在 Debian 系统上编译 GCC 9.3.0 上的程序,sizeof(max_align_t)在我的系统上是 32

-faligned-new=1 是什么意思?

显然,这意味着-faligned-new的使用没有明确的阈值。

根据文档,在这种情况下,阈值默认为std::max_align_t对齐。