Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Aliasing through a union pointer |
| Date | 2015-11-24 22:58 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <n32md3$v2d$1@dont-email.me> (permalink) |
| References | (2 earlier) <n2voss$m44$1@dont-email.me> <n2vtq1$avd$1@dont-email.me> <slrnn5749h.5q5.grahn+nntp@frailea.sa.invalid> <n318h6$6lq$1@dont-email.me> <slrnn58t2f.5q5.grahn+nntp@frailea.sa.invalid> |
On 24/11/15 15:29, Jorgen Grahn wrote: > On Tue, 2015-11-24, David Brown wrote: >> On 23/11/15 23:20, Jorgen Grahn wrote: >>> On Mon, 2015-11-23, David Brown wrote: >>> ... >>>> I'd advise against __attribute__((packed)) (or equivalents on other >>>> compilers) - either it makes no difference because there is no padding, >>>> or you will end up with structures with fields that don't match their >>>> alignment. On some targets, that means run-time errors - on others, it >>>> means seriously degraded code speed. So it is rarely a good idea. >>> >>> I think I agree, but are there really cases where you say >>> __attribute__((packed)) and then suffer a crash because of it? The >>> compiler shouldn't generate a struct, and code to access it which will >>> crash if it's ever run. >>> >> >> The attribute tells gcc that it should generate the struct layout >> exactly as /you/ tell it - /not/ as the target system normally requires >> with respect to alignment. > ... >> So if it is critical, you can use "packed" if you make sure it is safe >> to do so on /your/ target, with /your/ compiler, using /your/ access >> methods - and treat it as highly implementation-dependent. > > Makes sense. I just wish the gcc documentation had expressed it that > way, so I could point people to it when they abuse it. > >> Or just use explicit padding with a static assertion on the final size >> (or particular offsets, if you prefer) to make the code clear, safe, and >> portable (or at least giving clear error messages when you hit >> portability issues). > > I prefer reading uint8_t by uint8_t from a buffer in most cases where > I could have used a struct "overlay". But yes, your way works too in > many important cases (and importantly will not fail at runtime). > memcpy is good for doing such copies - it has the advantage that if it could be done faster using 16-bit or bigger moves, the compiler can do that (if it is reasonably smart), with no worries about aliasing :-) 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. And as noted before, be generous with your static assertions. You find out problems at compile time, with no run-time costs.
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