Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Struct casting |
| Date | 2011-12-05 17:13 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4EDD4217.6010900@verizon.net> (permalink) |
| References | <jbjeed$c1v$1@speranza.aioe.org> <1ff2e759-cf81-4eee-8278-50a3992fb7d9@x7g2000yqb.googlegroups.com> |
On 12/05/2011 05:06 PM, tom st denis wrote:
> On Dec 5, 4:52 pm, Edward Rutherford
> <edward.p.rutherfor...@REMOVETHIS.gmail.com> wrote:
>> This was pulled up at a code review. Code was similar to this:
>>
>> struct foo {
>> int bar;
>> int baz;
>>
>> } f;
>>
>> int * i = (int *) f;
>>
>> The only way this could go wrong is if the struct contains padding before
>> its first member, but I don't think this would ever happen in practice
>> --- maybe on theoretical perverse implementations like the ones you guys
>> like to play with :)
>>
>> Who is right here by the letter of the law?
>
> Is there a question of alignment though?
No, there is not. The standard guarantees that conversion of a pointer
to a struct to a pointer to the type of the first member produces a
valid pointer to that first member.
> Generally, that sort of code would fail review with me and my cohorts
> [er...coworkers]. If you wanted a specific element of a struct you
> should pick it by name.
Agreed. In addition to protecting against the possibility that bar might
be moved to a different position, it's also more type-safe, since a type
cast is no longer necessary.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Struct casting Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-05 21:52 +0000
Re: Struct casting Harald van Dijk <truedfx@gmail.com> - 2011-12-05 13:58 -0800
Re: Struct casting Tobias Blass <tobiasblass@gmx.net> - 2011-12-05 22:00 +0000
Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:10 +1300
Re: Struct casting tom st denis <tom@iahu.ca> - 2011-12-05 14:06 -0800
Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:11 +1300
Re: Struct casting James Kuyper <jameskuyper@verizon.net> - 2011-12-05 17:13 -0500
Re: Struct casting Robert Wessel <robertwessel2@yahoo.com> - 2011-12-05 16:10 -0600
Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:13 +1300
Re: Struct casting James Kuyper <jameskuyper@verizon.net> - 2011-12-05 17:15 -0500
Re: Struct casting Keith Thompson <kst-u@mib.org> - 2011-12-05 14:16 -0800
Re: Struct casting Lauri Alanko <la@iki.fi> - 2011-12-06 18:00 +0000
Re: Struct casting blp@cs.stanford.edu (Ben Pfaff) - 2011-12-05 14:19 -0800
Re: Struct casting jacob navia <jacob@spamsink.net> - 2011-12-06 08:29 +0100
Re: Struct casting Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-06 00:15 -0800
Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 22:30 +1300
Re: Struct casting jgk@panix.com (Joe keane) - 2011-12-08 21:08 +0000
csiph-web