New gcc support (alignment)

I'm having difficulty finding more information about the GCC-oriented new warning and the gcc -faligned-new option. Compiling on gcc 7.2.0 (without -std = C ++ 17) and attempting to define an aligned structure, such as:

struct alignas(64) Foo { int x; }

Just make a simple old one:

Foo * f = new Foo();

Gives me the following warning and suggestion:

 alignas.cpp:36:25: warning: ‘newof 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

I understand that by default newonly memory tied to will return alignof( std::max_align_t )(for me it is 16), but it is not clear to me that if I pass -faligned-new, will gcc now apply the proper new alignment newon my behalf?

Unfortunately, gcc documentation on this subject is sorely lacking.

+4
source share
1 answer

gcc:

-faligned
++ 17 new , , void* ::operator new(std::size_t). , -faligned-new = 32, , ( ) , alignof(std::max_align_t).

, -faligned-new P0035R4 ++ 17 .

++:
[cpp.predefined]:

__STDCPP_DEFAULT_NEW_ALIGNMENT__
    Integer 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). - ]

[basic.align/3]:

__STDCPP_DEFAULT_NEW_ALIGNMENT__

[expr.new/14]:

, . - std::size_t. , std::align_val_t.


, ++ 17 -faligned-new, Foo , Foo* f = new Foo(); void* operator new(size_t, align_val_t), Foo, 64- . .

+1

Source: https://habr.com/ru/post/1695080/


All Articles