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


Groups > comp.lang.c > #126883

Re: C Construction Problem

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: C Construction Problem
Date 2018-02-20 15:08 +0000
Organization A noiseless patient Spider
Message-ID <87po4z3dh5.fsf@bsb.me.uk> (permalink)
References <p6h95408re@news4.newsguy.com>

Show all headers | View raw


"F. Russell" <fr@random.info> writes:
<snip>
> The problem constructions are found in the file "pei.h" and a
> few examples are given here:
>
> #define peiGetOps(image)	  (((imageType*)(image))->ops)
> #define peiGetWidth(image)	  (((imageType*)(image))->width)
> #define peiGetHeight(image)	  (((imageType*)(image))->height)
> #define peiGetDepth(image)	  (((imageType*)(image))->depth)
> #define peiGetLinebytes(image)	  (((imageType*)(image))->linebytes)
>
> Here, the variable "image" is a pointer to the struct "imageType"
> which is defined earlier in the file.  (To those who can't download
> a copy of the source I will append the code for the struct at the
> end of this message.)
>
> I can't understand the need for these constructions.  To fix the program,
> among other things, I was able to rewrite these as follows:
>
> #define peiGetOps(image)	  image->ops
> #define peiGetWidth(image)	  (*image).width
> #define peiGetHeight(image)	  (*image).height
> #define peiGetDepth(image)	  (*image).depth
> #define peiGetLinebytes(image)	  image->linebytes

You should be able to use (image)->width and so on as well because the
x->y notation means (*x).y.

So while there is no need to use (*image).width, if you do, the advice
would be to write (*(image).width).  In fact, the usual advice would
include writing ((image)->ops).

The reason for the extra () is that macro expansion is literal and takes
no account of the syntax, the classic example being

#define SQUARE(x) x*x

where 2*SQUARE(1+2) expand to 2*1+2*1+2 which is 6 and not 18.  If you
write

#define SQUARE(x) ((x)*(x))

it all comes out right.  Now you can sometimes make a case that there is
nothing that can be passed the makes the ()s necessary, but it's much
simpler for readers of the code if the whole macro body and every macro
parameter is in ()s.

The original has a mysterious cast.  Are you sure you don't need it?  If
whatever gets passed to these macros is always a point to an imageType
object, the cast is superfluous, but then why is there at all?

> Note that these 5 lines are just a few of many examples of
> this kind.
>
> But even more puzzling (to me) are the following lines:
>
> #define peiGetConvertSrc(image) (*(imageType**)&(image)->clientData[0])
> #define peiGetConvertOp(image)  (*(int32_t*)&(image)->clientData[2])
>
> I can't even begin to deconstruct these.  Can someone explain what
> is going on?

First, you have to understand the operator precedence.  In C, all
postfix operators bind more tightly that prefix ones, so

  *(imageType**)&(image)->clientData[0]

is doing something baffling to image->clientData[0].  That's an int32_t
object.  Now, there's a nasty (and dangerous) C idiom

  *(T*)&X

where a pointer to X (that's &X) is converted to a pointer to some type
T (that's the (T*) cast) and then dereferenced (that's the outer *).
This blithely treats the bits in the object X as if there were of the
type T.  It may work, it may not!

In your case the type is a pointer type (i.e. T is "imageType*") so
there is an extra * in there but it's the same idea -- take an int32_t
and pretend it's a pointer.  On some 64-bit implementations this is a
non-no.  If pointers are 64 bits, and int32_t is just not big enough
for this to work properly.

There is another, optional, type -- intptr_32_t -- which, if provided,
is guaranteed to be big enough to hold a pointer.  You probably need to
use it here.

The second line rather mysteriously takes image->clientData[2], which is
of type int32_t and treats is as if it is an int32_t!  Maybe it was you,
in the conversion, that made image->clientData an array of int32_t?  If
not, it's an odd line.

> Here is the code for the imageType struct:
>
> struct imageType { [...]

and there is, presumably, a typedef:

  typedef struct imageType imageType;

?  If not, you are using C++ without knowing it!

-- 
Ben.

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


Thread

C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 13:51 +0000
  Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-20 14:42 +0000
    Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 15:06 +0000
      Re: C Construction Problem Manfred <noname@invalid.add> - 2018-02-20 18:07 +0100
  Re: C Construction Problem Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-20 14:54 +0000
  Re: C Construction Problem "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-20 10:06 -0500
    Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 16:11 +0000
  Re: C Construction Problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-20 15:08 +0000
    Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 16:22 +0000
      Re: C Construction Problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-20 17:03 +0000
        Re: C Construction Problem Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-20 09:23 -0800
        Re: C Construction Problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-20 11:11 -0700
        Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 18:30 +0000
          Re: C Construction Problem Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-20 23:37 +0000
            Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-21 00:45 +0000
              Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-21 01:46 +0000
                Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-21 08:49 +0100
                Re: C Construction Problem Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-21 09:29 +0000
                [OT] Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-21 11:24 +0000
                Re: [OT] Re: C Construction Problem Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-21 14:41 +0300
                Re: [OT] Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-21 15:10 +0100
                Re: [OT] Re: C Construction Problem Robert Wessel <robertwessel2@yahoo.com> - 2018-02-21 12:36 -0600
                Re: [OT] Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-21 21:04 +0000
                Re: [OT] Re: C Construction Problem Robert Wessel <robertwessel2@yahoo.com> - 2018-02-21 18:20 -0600
                Re: C Construction Problem Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-21 11:09 +0300
                Re: C Construction Problem Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-21 02:29 -0800
                Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-21 11:32 +0000
                Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-26 11:02 -0800
                Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-27 09:22 +0100
                Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-27 11:18 -0800
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-27 19:51 +0000
                Re: C Construction Problem Robert Wessel <robertwessel2@yahoo.com> - 2018-02-28 01:54 -0600
                Re: C Construction Problem Robert Wessel <robertwessel2@yahoo.com> - 2018-02-28 01:43 -0600
                Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-28 12:54 -0800
                Re: archive decompressor Noob <root@127.0.0.1> - 2018-03-04 12:34 +0100
              Re: C Construction Problem Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-21 11:04 +0300
                Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-21 11:27 +0000
                Re: C Construction Problem Anton Shepelev <anton.txt@gmail.com> - 2018-02-28 22:59 +0300
                Re: C Construction Problem supercat@casperkitty.com - 2018-02-21 08:49 -0800
            Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-26 11:08 -0800
              Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-26 23:51 +0000
                Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-27 11:24 -0800
          Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 08:07 -0800
            Re: C Construction Problem fr314159@gmail.com - 2018-02-21 09:16 -0800
              Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 21:26 -0800
            Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-21 09:43 -0800
              Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 22:47 -0800
                Re: C Construction Problem Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-22 13:09 +0000
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:37 -0800
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-22 10:50 -0800
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:58 -0800
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-24 15:05 -0800
                Re: C Construction Problem Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-25 03:21 -0800
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-25 11:51 +0000
                Re: C Construction Problem Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-25 04:06 -0800
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-25 12:47 +0000
                Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-25 13:20 +0000
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-25 17:46 +0000
                Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-25 18:58 +0000
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-25 20:01 +0000
                Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-25 20:25 +0000
                Re: C Construction Problem Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-26 02:49 -0800
                Re: C Construction Problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-25 20:02 +0000
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-25 15:13 -0800
                Re: C Construction Problem Robert Wessel <robertwessel2@yahoo.com> - 2018-02-25 11:50 -0600
                Re: C Construction Problem Ian Collins <ian-news@hotmail.com> - 2018-02-26 07:38 +1300
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-25 15:15 -0800
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-26 22:02 -0800
                Re: C Construction Problem jameskuyper@verizon.net - 2018-02-27 06:32 -0800
                Re: C Construction Problem GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-27 16:56 +0100
                Re: C Construction Problem jameskuyper@verizon.net - 2018-02-27 08:27 -0800
                Re: C Construction Problem GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-27 18:02 +0100
                Re: C Construction Problem jameskuyper@verizon.net - 2018-02-27 10:23 -0800
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 01:09 -0800
                Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-28 10:37 +0100
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 01:29 -0800
                Re: C Construction Problem Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 02:00 -0800
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-27 08:46 -0800
                Re: C Construction Problem jameskuyper@verizon.net - 2018-02-27 09:15 -0800
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 02:08 -0800
                Re: C Construction Problem Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 02:31 -0800
                Re: C Construction Problem Ian Collins <ian-news@hotmail.com> - 2018-03-04 10:34 +1300
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-19 10:27 -0700
                Re: C Construction Problem jameskuyper@verizon.net - 2018-03-03 21:35 -0800
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-17 10:29 -0700
                Re: C Construction Problem Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-18 08:14 -0700
                Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-25 21:49 +0100
                Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-25 15:09 -0800
                Re: C Construction Problem gazelle@shell.xmission.com (Kenny McCormack) - 2018-02-26 13:46 +0000
                Re: C Construction Problem David Brown <david.brown@hesbynett.no> - 2018-02-26 14:57 +0100
                Re: C Construction Problem Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-26 22:14 -0800
            Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-22 00:11 +0000
              Re: C Construction Problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-21 17:27 -0700
    Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-20 09:30 -0800
      Re: C Construction Problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-21 00:23 +0000
  Re: C Construction Problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-20 09:11 -0700
    Re: C Construction Problem "F. Russell" <fr@random.info> - 2018-02-20 16:38 +0000
      Re: C Construction Problem bartc <bc@freeuk.com> - 2018-02-20 17:07 +0000
      Re: C Construction Problem John Bode <jfbode1029@gmail.com> - 2018-02-20 09:09 -0800
      Re: C Construction Problem "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-20 12:18 -0500
        Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-20 09:38 -0800
      Re: C Construction Problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-20 11:08 -0700
      Re: C Construction Problem Gordon Burditt <gordon@hammy.burditt.org> - 2018-02-20 21:47 -0600
        Re: C Construction Problem Keith Thompson <kst-u@mib.org> - 2018-02-21 09:36 -0800
          Re: C Construction Problem Geoff <geoff@invalid.invalid> - 2018-02-26 11:21 -0800

csiph-web