Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #8664

Re: offsetOf

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: offsetOf
Date 2011-07-24 14:50 +0000
Organization Freie Universitaet Berlin
Message-ID <992pplFl9fU1@mid.uni-berlin.de> (permalink)
References <pan.2011.07.23.20.54.51@mailinator.com> <0.500382f6e840bc7bfed7.20110723230357BST.871uxgu01e.fsf@bsb.me.uk> <pan.2011.07.24.10.30.26@mailinator.com>

Show all headers | View raw


Jack Boot <jackboot@mailinator.com> wrote:
> Ben Bacarisse wrote:
> > Jack Boot <jackboot@mailinator.com> writes:
> > 
> >> This came up in a code review recently, I've never heard of it before,
> >> opinions welcome.
> >>
> >> There is a struct that looks a bit like this
> >>
> >> struct foo {
> >>   int a[10];
> >>   long b;
> >>   ...
> >> };
> >>
> >> Now I have a function that starts thusly:
> >>
> >> void
> >> bar ( struct foo * f )
> >> {
> >>   int * ip = (int *) foo;
> >>   ...
> >> }
> >>
> >> The reviewer recommended, that for maximal portability I should replace
> >> this by:
> >>
> >> int * ip = ( (int *) ( ( (unsigned char *) foo ) + offsetOf(struct foo,
> >> a) ) );
> >>
> >> I've never seen this construction before, also I think type punning is
> >> inherently non-portable so I'm not sure how much this buys you.
> > 
> > Nothing but obfuscation.   offsetOf(struct foo, a) is 0 (it can't be
> > otherwise) so this just converts to a char * first.  This won't hurt but
> > it won't help either.  This:
> > 
> >   int *ip = f->a;
> > 
> > is, to my mind, the clearest way to write it.  I can't see why it would
> > be done with any casts at all.

> Thanks for all the replies.

> I wrote the code with the cast because I used struct foo as an Opaque Type 
> in that file (only using pointers, the header with the struct definition 
> not #include'd)

In that case the code using the offsetof() macro can't compile,
since offsetof() needs to know how the structure looks like.
And it doesn't sound like a very useful kind of "encapsulation"
if you then retrieve something from the innards of the structure.

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

offsetOf Jack Boot <jackboot@mailinator.com> - 2011-07-23 20:54 +0000
  Re: offsetOf Ian Collins <ian-news@hotmail.com> - 2011-07-24 09:05 +1200
  Re: offsetOf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-23 23:03 +0100
    Re: offsetOf Jack Boot <jackboot@mailinator.com> - 2011-07-24 10:30 +0000
      Re: offsetOf Ian Collins <ian-news@hotmail.com> - 2011-07-24 22:41 +1200
      Re: offsetOf James Kuyper <jameskuyper@verizon.net> - 2011-07-24 08:36 -0400
      Re: offsetOf jt@toerring.de (Jens Thoms Toerring) - 2011-07-24 14:50 +0000
      Re: offsetOf Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-24 10:28 -0600
      Re: offsetOf Nobody <nobody@nowhere.com> - 2011-07-24 17:30 +0100
      Re: offsetOf gordonb.gkpc8@burditt.org (Gordon Burditt) - 2011-07-27 08:43 -0500
    Re: offsetOf Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-24 14:18 +0300
      Re: offsetOf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-24 12:50 +0100
        Re: offsetOf Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-24 18:25 +0300
  Re: offsetOf Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-23 20:12 -0400
  Re: offsetOf "J. J. Farrell" <jjf@bcs.org.uk> - 2011-07-24 01:57 +0100

csiph-web