Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Stephen Sprunk <stephen@sprunk.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Aliasing through a union pointer |
| Date | 2015-11-23 19:05 -0600 |
| Organization | A noiseless patient Spider |
| Message-ID | <n30d0d$5a0$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> <n3056v$8gk$1@dont-email.me> <slrnn5777g.5q5.grahn+nntp@frailea.sa.invalid> |
On 23-Nov-15 17:10, Jorgen Grahn wrote: > On Mon, 2015-11-23, Stephen Sprunk wrote: >> On 23-Nov-15 16:20, Jorgen Grahn wrote: >>> 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. >> >> As long as you access a misaligned member via a packed struct that >> is itself properly aligned, then GCC will generate the necessary >> code to safely do so. >> >> If the struct itself is misaligned, > > But that's broken code even if you don't use 'packed', right? Right, but it's worth mentioning anyway. GCC will only generate fixup code for members it _knows_ are misaligned due to packing, and only for the degree of misalignment that it expects. For instance, a 4-byte member expected to be 2-byte misaligned may not work if actually 1- or 3-byte misaligned--or vice versa. > (Although you tend to use 'packed' when you want to pretend a char > array is a struct Foo, so the two tend to go hand in hand.) In most cases where I've found this useful, the buffer was known to be properly aligned for the struct, so I could just cast a pointer and go. In general, one must memcpy() from the buffer into a separate struct object, but that is still usually easier than extracting each of the members individually from the buffer. >> or if you dereference a pointer to a misaligned member, then all >> bets are off. > > So saying &foo.bar suddenly becomes a bug waiting to happen, because > foo is a packed struct? Yes. Here be dragons. > Yes, I can see how that would be unavoidable. But surely a compiler > which provides 'packed' would emit a warning for that code? > ... > If it doesn't, then I agree about staying away from packed structs > (which I do anyway, but that strengthens my argument a lot). I'd hope so, but I wouldn't count on it. > In a sense foo.bar has no more of an address than a bitfield has. The former has an address that is merely misaligned; the latter has no address at all because individual bits aren't addressable. S -- Stephen Sprunk "God does not play dice." --Albert Einstein CCIE #3723 "God is an inveterate gambler, and He throws the K5SSS dice at every possible opportunity." --Stephen Hawking
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