Machine-translated by Google. Questions are authored in English.
Top

Discussion

Casting a byte buffer to a structure pointer to parse a network packet is unsafe chiefly because of

  • A.the structure having internal linkage
  • B.the compiler placing the structure in flash
  • C.alignment and padding assumptions
  • D.the buffer being declared volatile

Answer: C

Correct answer: alignment and padding assumptions

The compiler inserts padding to align members, and the layout it chooses need not match the packing of the network protocol. Worse, a byte buffer may not be aligned as the structure requires, and an unaligned access faults on some processors. Parsing field by field, or a packed structure with the endianness handled explicitly, is the safe route.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.