This will avoid the compiler padding for alignment issue. Pack the structure by writing all the bytes of each individual member to a byte array. Forbidding optimizations in production-quality code soĪs to make quick-and-dirty code work seems a poor tradeoff. Since there are better ways to manage I/O and better ways to manageĬhecksums. Yes, the "Variables matter, padding doesn't" view makes things The content of bytes that might not even exist! Little reason for the Standard to make guarantees about preserving Size and arrangement is entirely implementation-specific), there's Going to regard the padding as unimportant (and we should, since its Matters, not whatever padding may come along for the ride. In this view the contained variables are what The wider picture, I think, is that the Standard views a structĪs a sort of container for related variables, and not as a templateįor a span of memory. using one write to store two variables, at the cost of nailing There could also be optimizations like changing Processor and I don't want to increase the complexity of the software I know other better standard file formats for configuration settings areĪvailable (INI, XML. Read/write the struct on a different processor. I'm not interested in a standard format file (its aĬustom configuration for the application) and I'll never need to In my application (running onĪ single processor), I have to read/write the struct from/to a file and Yes, they are considerations I also made. > omitted from a checksum since they're not part of the "value." > generic batch of chars), the bytes after '\0' should probably be For that matter, if `z' holds a string (as opposed to a > bit-field elements: The values of un-named bits are not necessarily > assigning to struct elements, a checksum that includes padding > necessarily preserved when assigning structs or even when > bytes - the size of `z' in your example almost begs for some > checksum computation will include the values of any padding > A problem with the approach you've outlined is that the Values to addressable local variables before applying the low. ![]() Such a function could even handle bit-fields by copying their Unsigned int PSChecksum(const struct PStruct *) Winds up making the cumbersome call(s) to the true underlying Specifically for struct PStruct objects, even if that function Handle bit-fields since you can't point at them.Ī preferable approach would be to write a checksum function Somebody could omit a field by accident, or (for checksums thatĪre non-commutative) get them in the wrong order. It seems to me this would be cumbersome, and also prone to error: One possibility would be to checksum the fields individually, Omitted from a checksum since they're not part of the "value." Generic batch of chars), the bytes after '\0' should probably be Similar concerns apply toīit-field elements: The values of un-named bits are not necessarily Necessarily preserved when assigning structs or even whenĪssigning to struct elements, a checksum that includes paddingīytes is unlikely to be very useful. A problem with the approach you've outlined is that theĬhecksum computation will include the values of any paddingīytes - the size of `z' in your example almost begs for some
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |