alignas

How to use alignas to replace pragma pack?

时间秒杀一切 提交于 2021-02-06 15:15:43
问题 I am trying to understand how alignas should be used, I wonder if it can be a replacement for pragma pack, I have tried hard to verify it but with no luck. Using gcc 4.8.1 (http://ideone.com/04mxpI) I always get 8 bytes for below STestAlignas, while with pragma pack it is 5 bytes. What I would like ot achive is to make sizeof(STestAlignas) return 5. I tried running this code on clang 3.3 (http://gcc.godbolt.org/) but I got error: !!error: requested alignment is less than minimum alignment of

aligned_malloc() vs alignas() for Constant Buffers

懵懂的女人 提交于 2021-02-05 08:59:06
问题 In C++, we have the keyword alignas(n) and we have the _aligned_malloc(m,n) function. alignas works on the type while aligned_malloc works on whatever you call it. Can I use alignas(16) to fullfil the 16-byte alignment requirement for Direct3D Constant Buffers? 回答1: Yes, you could use it like this: struct SceneConstantBuffer { alignas(16) DirectX::XMFLOAT4X4 ViewProjection[2]; alignas(16) DirectX::XMFLOAT4 EyePosition[2]; alignas(16) DirectX::XMFLOAT3 LightDirection{}; alignas(16) DirectX:

alignas() effect on sizeof() - mandatory?

女生的网名这么多〃 提交于 2021-01-27 15:55:44
问题 This program: struct alignas(4) foo {}; int main() { return sizeof(foo); } returns 4, with GCC 10.1 and clang 10.1, and icc 19.0.1 . That makes me wonder - is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at? Or - is this change just the implementation's prerogative? 回答1: is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at?

alignas() effect on sizeof() - mandatory?

此生再无相见时 提交于 2021-01-27 15:40:42
问题 This program: struct alignas(4) foo {}; int main() { return sizeof(foo); } returns 4, with GCC 10.1 and clang 10.1, and icc 19.0.1 . That makes me wonder - is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at? Or - is this change just the implementation's prerogative? 回答1: is it mandatory for alignas() to affect sizeof() this way? i.e. increase the size beyond what the structure would originally be sized at?

Alignment of a simple class to allow array access without UB

情到浓时终转凉″ 提交于 2020-06-13 07:54:50
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

不想你离开。 提交于 2020-06-13 07:54:08
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

牧云@^-^@ 提交于 2020-06-13 07:53:47
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Align/offset specific members of a struct

梦想的初衷 提交于 2020-06-13 05:47:10
问题 What is the best or conventional method to align members inside a structure? Is adding dummy arrays the best solution? I have a struct of double and a triple of double s? struct particle{ double mass; std::tuple<double, double, double> position; } If I have an array of these, the memory will look like this [d][d d d][d][d d d][d][d d d]... The problem is that the distance from the first triple to the second triple is not an integer multiple of sizeof(std::tuple<double, double,double>)==3

treating memory returned by operator new(sizeof(T) * N) as an array

亡梦爱人 提交于 2020-01-10 01:34:07
问题 In C one can allocate dynamic arrays using malloc(sizeof(T) * N) and then use pointer arithmetic to get elements at i offset in this dynamic array. In C++ one can do similar using operator new() in the same way as malloc() and then placement new (for an example one can see solution for item 13 in a book "Exceptional C++: 47 engineering puzzles, programming problems, and solutions" by Herb Sutter). If you don't have one, the summary of the solution for this question would be: T* storage =

gcc over-aligned new support (alignas )

橙三吉。 提交于 2019-12-19 05:44:20
问题 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(); Gives me the following warning and suggestion: 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