compilation - Explanation of packed attribute in C -
i wondering if offer more full explanation meaning of packed attribute used in bitmap example in pset4.
"our use, incidentally, of attribute called packed ensures clang not try "word-align" members (whereby address of each member’s first byte multiple of 4),
lest end "gaps" in our structs don’t exist on disk."
i not understand comment around gaps in our structs. refer gaps in memory location between each struct (i.e. 1 byte between each 3 byte rgb if word-algin)? why matter in optimization?
typedef uint8_t byte; typedef struct { byte rgbtblue; byte rgbtgreen; byte rgbtred; } __attribute__((__packed__)) rgbtriple;
beware: prejudices on display!
as noted in comments, when compiler adds padding structure, improve performance. uses alignments structure elements give best performance.
not long ago, dec alpha chips handle 'unaligned memory request' (umr
) doing page fault, jumping kernel, fiddling bytes required result, , returning correct result. painfully slow comparison correctly aligned memory request; avoided such behaviour @ costs.
other risc chips (used to) give sigbus error if misaligned memory accesses. intel chips have fancy footwork deal misaligned memory accesses.
the purpose of removing padding (decrease performance but) benefit being able serialize , unserialize data without doing job 'properly' — form of laziness doesn't work when machines communicating not of same type, proper serialization should have been done in first place.
what mean if writing data on network, seems simpler able send data writing contents of structure block of memory (error checking etc omitted):
write(fd, &structure, sizeof(structure));
the receiving end can read data:
read(fd, &structure, sizeof(structure));
however, if machines of different types (for example, 1 has intel cpu , other sparc or power cpu), interpretation of data in structures vary between 2 machines (unless every element of array either char
or array of char
). relay information reliably, have agree on byte order (e.g. network byte order — factor in tcp/ip networking, example), , data should transmitted in agreed upon order both ends can understand other saying.
you can define other mechanisms: use 'sender makes right' mechanism, in 'receiver' let's sender know how wants data presented , sender responsible fixing transmitted data. can use 'receiver makes right' mechanism works other way around. both these have been used commercially — see drda 1 such protocol.
given type of byte
uint8_t
, there won't padding in structure in sane (commercially viable) compiler. imo, precaution fantasy or phobia without basis in reality. i'd need documented counter-example believe there's actual problem attribute helps with.
i led believe encounter issues when pass entire struct function
fread
assumes you're giving array chunk of memory, no gaps in it. if struct has gaps, first byte ends in right place, next 2 bytes written in gap, don't have proper way access.
sorta...but no. issue values in padding bytes indeterminate. however, in structure shown, there no padding in compiler i've come across; structure 3 bytes long. there no reason put padding anywhere inside structure (between elements) or after last element (and standard prohibits padding before first element). so, in context, there no issue.
if write binary data file , has holes in it, arbitrary byte values written holes are. if read on same (type of) machine, there won't problem. if read on different (type of) machine, there may problems — hence comments serialization , deserialization. i've been programming in c little on 30 years; i've never needed packed, , don't expect to. (and yes, i've dealt serialization , deserialization using standard layout — system worked on used big-endian data transfer, corresponds network byte order.)
Comments
Post a Comment