gcc过度对齐的新支持(alignas) [英] gcc over-aligned new support (alignas )

查看:549
本文介绍了gcc过度对齐的新支持(alignas)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到有关GCC新对齐警告和gcc -faligned-new选项的更多信息.在gcc 7.2.0上编译(不带--std = c ++ 17),并尝试定义对齐的结构,例如:

I'm having some difficulty finding more information about GCC's aligned-new warning and the gcc -faligned-new option. Compiling on gcc 7.2.0 (without --std=c++17) and trying to define an aligned struct such as:

struct alignas(64) Foo { int x; }

只需做一个普通的老话:

Just doing a plain old:

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仅返回对齐至alignof( std::max_align_t )的内存(对我来说这是16),但是我不清楚的是,如果我通过-faligned-new,gcc现在将强制执行正确的操作代表我new的新对齐方式?

I understand that by default new will only return memory aligned up to alignof( std::max_align_t ) ( which is 16 for me ), but what's not clear to me is that if I pass -faligned-new, will gcc now enforce proper new alignment of new on my behalf?

不幸的是,关于此的gcc文档非常缺乏.

Unfortunately the gcc documentation on this is extremely lacking.

推荐答案

来自

-faligned-new
启用对类型比void* ::operator new(std::size_t)提供的更多对齐要求的C ++ 17 new的支持.可以使用-faligned-new = 32之类的数字参数来指定该函数提供的对齐方式(以字节为单位),但是很少有用户需要覆盖默认值alignof(std::max_align_t).

-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仅会使

This implies that -faligned-new simply makes the aligned-new features added in P0035R4 available without fully enabling C++17 support.

C ++标准中的相关位:
来自[cpp.predefined]:

The relevant bits from the C++ standard:
From [cpp.predefined]:

__STDCPP_DEFAULT_NEW_ALIGNMENT__
类型为std::size_t的整数文字,其值是通过调用operator new(std::size_t)operator new[](std::size_t)保证的对齐方式. [注意:较大的对齐方式将传递给operator new(std::size_t, std::align_val_t)等(8.3.4). —尾注]

__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 ]

来自[basic.align/3]:

From [basic.align/3]:

新扩展的比对由比__STDCPP_DEFAULT_NEW_ALIGNMENT__

来自[expr.new/14]:

And from [expr.new/14]:

对通过组合参数列表创建的函数调用执行重载解析.第一个参数是请求的空间量,类型为std::size_t.如果分配的对象的类型具有新扩展的对齐方式,则下一个参数是该类型的对齐方式,其类型为std::align_val_t.

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)分配内存并返回指针到在64字节边界上正确对齐的Foo对象.在较早的标准下情况并非如此.


So in your case with C++17 or -faligned-new, since Foo has new-extended alignment, Foo* f = new Foo(); will call void* operator new(size_t, align_val_t) to allocate memory and return a pointer to a Foo object that is properly aligned on a 64-byte boundary. Under earlier standards that was not the case.

这篇关于gcc过度对齐的新支持(alignas)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆