Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Aliasing through a union pointer |
| Date | 2015-11-25 12:57 +1300 |
| Message-ID | <dbkbrsFjpqnU25@mid.individual.net> (permalink) |
| References | (4 earlier) <slrnn5749h.5q5.grahn+nntp@frailea.sa.invalid> <n318h6$6lq$1@dont-email.me> <slrnn58t2f.5q5.grahn+nntp@frailea.sa.invalid> <n32md3$v2d$1@dont-email.me> <n32oaa$789$1@dont-email.me> |
Eric Sosman wrote: > On 11/24/2015 4:58 PM, David Brown wrote: >> [... "packed" structs and similar underhanded artifices ...] >> Also explicit packing "dummies" have the advantage that there is no >> chance of undefined, implementation-defined, unspecified or generally >> unexpected or undesired behaviour when they are accessed. (When storing >> something in a struct, padding bytes get unspecified values.) This is >> particularly important if you want to compare structs, or do something >> like calculate a checksum over them. > > It's been my experience that these questions of packing and so on > nearly always arise in connection with an externally-imposed format of > some kind: You want to use a struct to represent an IP header or a > disk partition table entry or something of that sort. If that's the > situation, I've almost always found that trying to sculpt the struct > into the desired shape is inferior to just letting the struct take its > own preferred form and using a mediating buffer to translate to and > from the imposed format. Use separate "native" and "serialized" forms, > and you're quite likely to be better off than by trying to force both > forms into one object. > > Not only does the separation of forms avoid all the alignment > issues discussed elsethread, but it also offers a convenient hook for > dealing with other matters. Does the external form require a specific > endianness that may or may not match your machine's? Does it use a > weird representation for negative numbers, or for floating-point > quantities? Are text strings supposed to be a count word followed > by a payload in some strange five-bit encoding? The import() and > export() functions can handle all such idiosyncracies cleanly and > quietly, so the rest of the program can deal with perfectly normal, > native-format structs with native-format numbers, zero-terminated > native-encoded strings, and so on. The translation functions are > also good places to compute and verify things like checksums, which > are usually specified in terms of the external format. > > Somewhere, somebody is shrieking "Efficiency! All that copying? > Why should I un-pickle an entire object just to test the one field > that tells me whether I care about the others? Eeeek!" This shrieker > is overly fearful and insufficiently imaginative; there are plenty of > ways to attack such issues if they should prove important. (Also, > for some reason the shrieker seldom yowls about the speed penalties > of using the misaligned elements in his packed structs. Go figure.) > > There are many situations where it is tempting to map an imposed > format with a struct, but resisting temptation is nearly always a > virtue, and a virtue with rewards. I agree with you here. I leaned that lesson many years ago when testing embedded code on SPARC systems. The cost of a misaligned access was way more than the cost of unpacking the messages (and there were a lot of them!). The "Why should I un-pickle an entire object just to test the one field?" problem can be solved by lazy extraction: only extract that one field and keep the raw buffer around to be processed later. -- Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Aliasing through a union pointer Ian Pilcher <arequipeno@gmail.com> - 2015-11-23 10:14 -0600
Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 11:43 -0500
Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 11:49 -0500
Re: Aliasing through a union pointer Ian Pilcher <arequipeno@gmail.com> - 2015-11-23 13:22 -0600
Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 14:38 -0500
Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-25 10:38 -0800
Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-25 14:17 -0500
Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:16 -0800
Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-23 21:46 +0100
Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-23 22:20 +0000
Re: Aliasing through a union pointer Stephen Sprunk <stephen@sprunk.org> - 2015-11-23 16:52 -0600
Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-23 23:10 +0000
Re: Aliasing through a union pointer Stephen Sprunk <stephen@sprunk.org> - 2015-11-23 19:05 -0600
Re: Aliasing through a union pointer Nick Bowler <nbowler@draconx.ca> - 2015-11-23 22:51 +0000
Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-23 15:49 -0800
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-23 16:01 -0800
Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 09:55 +0100
Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-24 14:29 +0000
Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 22:58 +0100
Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-24 17:30 -0500
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 15:27 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-25 12:57 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 16:31 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-25 18:28 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 06:54 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 08:59 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 13:01 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 12:58 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 16:44 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 14:12 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 21:34 -0800
Re: Aliasing through a union pointer Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-25 21:55 -0800
Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 20:51 +1300
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-26 11:39 -0800
Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-26 00:06 +0000
Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-24 11:51 -0800
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 12:20 -0800
Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-24 21:52 -0800
Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 08:30 -0800
Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 23:04 +0100
Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-25 10:10 -0800
Re: Aliasing through a union pointer Richard Damon <Richard@Damon-Family.org> - 2015-11-25 11:02 -0800
Re: Aliasing through a union pointer Philip Lantz <prl@canterey.us> - 2015-11-26 13:06 -0800
Re: Aliasing through a union pointer Richard Damon <Richard@Damon-Family.org> - 2015-11-26 13:09 -0800
Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:08 -0800
csiph-web