c - Alignment of byte array in struct -


i wondering standard how following structure aligned , allocated:

struct {    uint8_t b[16]; }; 

to extend can expect a of size 16 bytes, aligned on 16 byte boundary in array (and size of a[n] equal 16*n, e.g. no padding) , address of a first element of b?

and narrow scope bit, platforms concerned x86/x64, arm m, r, , a50.

edit: clarification - "alignment" mean how compiler align objects in array on stack, e.g. each next element 16 bytes away, naturally not expect malloc aligned on platform default boundary.

edit 2: after reading on answer, feel inclined ask, extend can expect platform-specific explicit alignment intrinsics work?

the size of struct @ least 16. 16 in normal c implementations, not see c standard requires this, absent extenuating circumstances such special requests user. e.g., if user uses gcc’s extensions specify struct must have alignment of 32 bytes, struct have padded 32 bytes.

the struct cannot expected have alignment requirement of 16. none of members have alignment requirement greater 1, , c standard not require struct have greater alignment requirement members. not see c standard requires structure not have greater alignment requirement minimum needed satisfy requirements of members, might possible c implementation give struct alignment of 16. unaware of c implementations absent special request user.

technically, pointer struct cannot same pointer first element of b, because former pointer struct , latter pointer char. however, when pointer struct converted pointer uint8_t [16], same (will compare equal to) address of b.

in every c implementation familiar with, when either pointer struct or pointer array further converted pointer uint8_t, same (will compare equal to) pointer first element of b. however, not see c standard guarantees this. there no padding @ start of struct or array, know “address” of struct same “address” of array , “address” of first element, not same guarantee converting pointers of different types produces proper value.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -