Questions tagged [alignas]

alignas is an attribute introduced in C++11, which informs the compiler that the attributed object shall have a certain memory alignment. It replaces non-portable, compiler-specific alignment attributing, such as __declspec(align(16)).

37 questions
36
votes
4 answers

Where can I use alignas() in C++11?

In an effort to standardize my code and make it more portable, I replaced #ifdef __GNUC__ typedef __attribute__((aligned(16))) float aligned_block[4]; #else typedef __declspec(align(16)) float aligned_block[4]; #endif with typedef float alignas(16)…
Walter
  • 44,150
  • 20
  • 113
  • 196
18
votes
3 answers

How to use alignas to replace pragma pack?

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…
marcinj
  • 48,511
  • 9
  • 79
  • 100
13
votes
1 answer

gcc over-aligned new support (alignas )

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;…
ByteMe95
  • 836
  • 1
  • 6
  • 18
11
votes
4 answers

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

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…
xor256
  • 197
  • 9
8
votes
1 answer

alignas in structs on 32-bit platforms

I am getting unexpected results when running the following code for 32-bit x86 linux (compiler flags: g++ -std=c++14 -m32). I tried gcc and clang. #include using namespace std; struct S1 { uint64_t a; uint32_t b; }; struct S2 { …
user3809741
  • 121
  • 2
8
votes
1 answer

Struggling with alignas syntax

I am trying to use alignas for pointers that are class members, and frankly I am not sure where I supposed to put it. For instance: class A { private: int n; alignas(64) double* ptr; public: A(const int num) : n(num), ptr(new…
user1683586
  • 373
  • 2
  • 12
7
votes
2 answers

alignas keyword not respected

I want to overalign my type on a cache boundary, so I used alignas: struct alignas(64) W { }; This compiles fine. But then, to my surprise, when I try to allocate a bunch of Ws, they're not 64-byte aligned but actually 16-byte aligned: #include…
Barry
  • 286,269
  • 29
  • 621
  • 977
5
votes
2 answers

How to replace aligned_storage with std::byte[]?

The use of aligned_storage is deprecated in C++23 and suggested to be replaced with an aligned std::byte[] (see here). I have two questions about this: 1. How to align this? The document suggests to replace std::aligned_storage_t
TilmannZ
  • 1,784
  • 11
  • 18
4
votes
1 answer

alignas() effect on sizeof() - mandatory?

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…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
1 answer

Is gcc missing an optimization opportunity on this comparison of overaligned struct of 2 shorts?

I was wondering if GCC will use the knowledge that the struct I created is overaligned and consider it simply as an integer when it comes to the generation of comparison asm. It does not. I wonder if this is due to ABI issues, some other factor I…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
4
votes
1 answer

How to create std::vector of char/std::byte where first byte is aligned to 16 byes, but there is no padding?

There is an existing question that requires C++03 and has no answer, so I will open a new one. Problem I have is that I want to have std::vector of std::byte, but so that .data()(first element of data array) is 16 byte aligned. alignas on wrapped…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
4
votes
1 answer

Do I need alignment in C++?

I am sure I do. The matrix library Eigen3 is likely much faster than any of my own matrix implementations thanks to alignment. I recently started investigating on alignment using C++. I stumbled on the alignas and alignof functions, and decided to…
mfnx
  • 2,894
  • 1
  • 12
  • 28
4
votes
0 answers

Why doesn't alignas compile when used in a static declaration with clang?

I have a compilation error on clang and a warning with gcc with this code: static alignas(16) int one_s = 1; // clang: error: an attribute list cannot appear here; gcc: warning: attribute ignored; static…
bluespeck
  • 173
  • 7
3
votes
2 answers

Is it correct to use alignas(16) for an array[8] of m128?

This is the code I usually write: alignas(16) __m128 myBuffer[8]; But maybe (since the object-array is 128*8 bit = 128 byte) I should write: alignas(128) __m128 myBuffer[8]; Or, "since the first 16 byte are aligned" in the first example, the rest…
markzzz
  • 47,390
  • 120
  • 299
  • 507
3
votes
2 answers

How does std::alignas optimize the performance of a program?

In 32-bit machine, One memory read cycle gets 4 bytes of data. So for reading below buffer, It should take 32 read-cycle to read a buffer of 128 bytes mentioned below. char buffer[128]; Now, Suppose if I have aligned this buffer as mentioned below…
Gaurav
  • 161
  • 1
  • 11
1
2 3