user2762996
user2762996

Reputation: 626

What does __attribute__ ((aligned(x))) do?

NONE of the existing answer to that question made anything clearer for me. I don't know what it's supposed to mean when a variable is declared "within the boundary of x bytes", which is the answer given in every question regarding this. I need an actual explanation with example of what it is, and what it does compared to variables declared without that attribute, and why it should be used.

Bonus question: When used in front of an array like here __attribute__ ((aligned (32))) double output[4];, does that mean every single element of the array is aligned, or just the array as a whole?

Thanks in advance.

Upvotes: 2

Views: 1742

Answers (1)

eerorika
eerorika

Reputation: 238341

There is no such thing as __attribute__ in the C++ language. __attribute__ is a language extension of GCC and compatible compilers.

__attribute__ ((aligned (X))) is essentially same as the standard alignas specifier:

// non-standard
__attribute__ ((aligned (32))) double output[4];

// standard
alignas(32) double output[4];

Every object has a memory address that designates the location of the byte in memory where the object is stored. The underlying representation of the address is an integer number. A memory address is aligned to X bytes when the number of the address is divisible by X.

Every type has a minimum requirement for the alignment of the address where an object of that type can be stored. alignas can be used to increase the requirement.

When used in front of an array ... does that mean every single element of the array is aligned, or just the array as a whole?

The array as a whole is aligned. Not individual elements

Upvotes: 5

Related Questions