简体   繁体   中英

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.

There is no such thing as __attribute__ in the C++ language. __attribute__ 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 address where the object can be stored. alignas can be used to increase the requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM