Reputation: 77
What determines C/C++ Memory Alignment Syntax?
I am working on some embedded application, which requires memory/data alignment. From my previous experience, for C code, with GCC and MSVC, the memory alignment syntax seems to be mainly determined by the compiler:
GCC __attribute__ ((aligned (128)))
MSVC __declspec(align(128))
I am curious about the following questions:
alignas(128)
part of the C++11
standard and thus independent of compiler/platform?std::align
on data, will it do a full data
copy and thus hinder the performance?Upvotes: 1
Views: 744
Reputation: 117831
Yes, alignas is a C++ keyword since C++11, but note:
If the strictest (largest) alignas on a declaration is weaker than the alignment it would have without any
alignas
specifiers (that is, weaker than its natural alignment or weaker thanalignas
on another declaration of the same object or type), the program is ill-formed
Since types have different alignments on different platforms, this may work for one target platform and fail for another:
struct alignas(2) foo { int bar; };
size_t
space counter if needed.
If the buffer is too small, the function does nothing and returns
nullptr
Upvotes: 1