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)).
Questions tagged [alignas]
37 questions
3
votes
1 answer
gcc alignas issue with member pointers to objects
I am using gcc 4.9.2 and I am trying to properly align statically initialized arrays for use with AVX. Here is the gist of the code that segfaults due to alignment issues:
#include
#include
struct B {
alignas(32) double…

kounoupis
- 329
- 1
- 10
3
votes
1 answer
alignas(T) not resolved in template function
When use Eclipse with MinGW(version:4.8.1) to compile the following code fragement, it can pass but Eclipse still report:
"Multiple markers at this line - Type 'alignas' could not be resolved"
template void set_aside(std::vector vx)…

Cendey
- 31
- 1
2
votes
1 answer
C++ class specifier alignas option via template
Can C++ template parameters be used to control specifiers on the class itself to minimize some code duplication?
For example: I have a class that I'd like to use both in a concurrent context (and container) with the alignas specifier, and also in a…

Kulluk007
- 902
- 2
- 10
- 24
2
votes
1 answer
aligned_malloc() vs alignas() for Constant Buffers
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…

Raildex
- 3,406
- 1
- 18
- 42
2
votes
1 answer
Align/offset specific members of a struct
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 doubles?
struct particle{
double mass;
std::tuple…

alfC
- 14,261
- 4
- 67
- 118
2
votes
1 answer
memory alignment higher than maximum alignment alignas malloc
How would one go about using malloc (or new, since on most implementations new is implemented with malloc, not sure what the standard says about alignment and new other than data has to be aligned with the highest scalar alignment) with a type that…

Curious
- 20,870
- 8
- 61
- 146
2
votes
1 answer
alignas specifier: on the type / on the member data
Are the following two cases equivalent in all situations?
1) alignas on the type
template
struct alignas(Alignment) A
{
T data[N];
};
2) alignas on the member array
template

plasmacel
- 8,183
- 7
- 53
- 101
2
votes
1 answer
Use alignas to align struct
In the following struct:
struct alignas(?) test
{
int32_t f1; // 4 bytes
int8_t f2; // 1 byte
int8_t f3; // 1 byte
};
How to use alignas so that sizeof(test) would be exactly 6 bytes?
alignas(1) is not accepted by compiler (gcc, msvc,…

vladon
- 8,158
- 2
- 47
- 91
1
vote
0 answers
Alignment of a simple class to allow array access without UB
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}{}
…

alfC
- 14,261
- 4
- 67
- 118
1
vote
1 answer
How exactly does alignment impact memory layout and the bahaviour of placement new?
We read a lot about alignment and how important it is, for example for placement new usage, but I was wondering - how does it exactly alter the layout of the memory?
Obviously, if we do
char buffer[10];
std::cout << sizeof buffer;
and
alignas(int)…

Fureeish
- 12,533
- 4
- 32
- 62
1
vote
3 answers
How to specify a pointer which points to unaligned int?
struct Intx4 {
int data[4];
};
Intx4 loadIntx4(void const *p) {
auto up alignas(1) = (int const *)p; // Does this line correct? (compiled ok in clang)
Intx4 r;
for (int i = 0; i < 4; i++) r.data[i] = up[i];
return r;
}
I also tried the…

wincmder
- 61
- 1
- 3
0
votes
1 answer
align array elements differently in aligned union
I'm using SSE/AVX and I need to store aligned data. how ever, my data can be of different types. so I use a union like this
union Data
{
bool Bools[128];
int32 Ints32[128];
int64 Ints64[128];
// ... other data types
} data;
Can I do…

M.kazem Akhgary
- 18,645
- 8
- 57
- 118
0
votes
0 answers
Can alignas be implemented with mmap?
I was wondering if someone can try to implement alignas with mmap by playing with the first parameter? If not, then can anyone try to implement alignas themselves?

FariyaAchhab
- 43
- 4
0
votes
2 answers
why alignas(64) not aligned with 64
why alignas(64) not aligned with 64? for example:
struct alignas(32) st32
{
float a;
uint16_t b;
uint64_t c;
};
struct alignas(64) st64
{
float a;
uint16_t b;
uint64_t c;
};
int main()
{
st32 x;
st64 y;
…

daohu527
- 452
- 4
- 15
0
votes
1 answer
What is the correct usage/syntax for the c++17 alignas() specifier for dynamically allocated arrays of fundamental types?
This must be a repeat question, but I have not found it after searching for 2 days ...
I'm using MSVC with /std:c17 /std:c++17 and trying to get alignas(64) to work with arrays of doubles. The syntax in the code below is the only one I have found…

dts
- 125
- 1
- 10