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


Groups > comp.lang.c > #97980

Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs]

From Anton Shepelev <anton.txt@gmail.com>
Newsgroups comp.lang.c
Subject Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs]
Date 2016-12-04 18:48 +0300
Organization A noiseless patient Spider
Message-ID <20161204184801.3873269617033a53467cd5e1@gmail.com> (permalink)
References (2 earlier) <69c8a6e9-9685-6047-21fd-0721bcf899a1@verizon.net> <20161122232309.40aa1289366c35d048c594f3@gmail.com> <2985f44b-b799-f6bb-142e-f2c1eb9d7ee2@verizon.net> <20161130001448.0acf489caa3aa31becdd5e2c@gmail.com> <67f7b425-b642-398d-409b-0e767fc1ebcf@verizon.net>

Show all headers | View raw


James R. Kuyper to Anton Shepelev:

> > > On page 34, he writes:
> > > struct Circle { const struct Point _; int rad;
> > > };
> > >
> > > Given
> > >
> > >    struct Circle c, *pc=&c;
> > >    struct Point p, *pp=&p;
> > >
> > > The explicit conversion he referred to is
> > > (const struct Point *)pc ,
> > > and it is allowed by 6.7.2.1p15.
> > >
> > > However, he's wrong about void* being the only
> > > other  way  to  do this. &pc->_ is simpler and
> > > equivalent, and will continue to work even  if
> > > some  other  field becomes the first member of
> > > struct Circle.
> >
> > In OOP this method is out of the  question,  be-
> > cause  a member should be accessible immediately
> > from the object at hand, without knowing the in-
> > heritance level at which it has been introduced.
> > Whereas the ->_ in your example betrays the  ob-
> > ject's  internal  structure  and  the assumption
> > that it is derived from Point.  What should  you
> > do   if   there   were   a   three-tier  hierar-
> > chy -- ->_->_?  For aught I know,  this  is  al-
> > lowed under the hood but in user code...
>
> The cast makes the assumption that the first actu-
> al member of struct Circle is an  object  of  type
> struct  Point, or an array of struct Point --  ei-
> ther directly, or recursively  because  the  first
> member  has  a  struct type of which that is true.
> The use of ->_, on the other hand, makes  the  as-
> sumption  that there is some member of struct Cir-
> cle, not necessarily the first one, with a name of
> _ and type of struct Point.

Right.

> Is  making an assumption about the location of the
> struct Point member any better, or any worse, than
> making  an  assumption about the name of that mem-
> ber?  That depends entirely upon which  assumption
> is more likely to be true.

I should rather say, on one's chosen design, because
mixing both way of access it likely a bad idea  any-
way,  and  code should be stuctured to guarantee the
correctness of one of them.

> If you're certain that one  assumption  is  valid,
> and  are  worried that the other one might not be,
> you should use the expression  which  relies  upon
> the  assumption  your  certain  of.  Otherwise, it
> doesn't matter.

It's not what I am worried about.  Rather,  I  doubt
that the by-member access agrees with the concept of
OOP's hierarchical data structures, where  the  mem-
bers  are accessed in a uniform way indifferently of
the object's line of ascent.  For example, the  syn-
tax  for accessing the coordinates of a Circle shall
not depend on the way Circle is derived, e.g.:

   Object => Circle
   (coordinates defined in the Cirlce)

   Object => Point => Circle
   (coordinates defined in Point)

   Object => RadialFigure => Point => Circle
   (coordinates defined in RadialFigure)

If that rule be violated, changing  the  implementa-
tion of Circle may break the code that uses it.

> > In  the  appendix  "ANSI-C Programming Hints" he
> > shows stark innocence of  the  aliasing  limita-
> > tions:
> >
> >    The  first  component  of  a structure starts
> >    right at  the  beginning  of  the  structure;
> >    therefore,  structures  can  be lengthened or
> >    shortened:
> >
> >       struct a { int x; };
> >       struct c { struct a a; ... } c, * cp = & c;
> >       struct a * ap = & c.a;
> >
> >    ANSI-C permits neither  implicit  conversions
> >    of   pointers  to  different  structures  nor
> >    direct access to the components of  an  inner
> >    structure:
> >
> >       cp -> a.x ok, fully specified
> >       ((struct a *) cp) -> x ok, explicit conversion (!!!)
> >
> >
> > Or am missing something?
>
> Possibly.   Or  maybe  I'm  missing  something.  I
> don't see any aliasing problems in the above code.
> Could you please identify them?

Ok.   My  initial  concern  was  about aliasing, but
since conversion here is made in-place, it cannot be
a  problem, because there is no second pointer vari-
able pointing to the same address.  Would this:

   ap = (struct a *) cp; /* alias to cp */
   ap -> x = 10;

be wrong?

-- 
()  ascii ribbon campaign - against html e-mail
/\  http://preview.tinyurl.com/qcy6mjc [archived]

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


Thread

Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-01 13:15 +0100
  Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-01 13:34 +0100
    Re: Aliasing and subtyping structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 15:27 +0000
      Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-02 09:47 +0100
        Re: Aliasing and subtyping structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-02 15:47 +0000
          Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-08 00:24 +0100
            Re: Aliasing and subtyping structs supercat@casperkitty.com - 2016-11-07 21:42 -0800
        Re: Aliasing and subtyping structs <william@wilbur.25thandClement.com> - 2016-11-03 18:53 -0700
          Re: Aliasing and subtyping structs supercat@casperkitty.com - 2016-11-04 09:56 -0700
          Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-08 21:53 +0100
  Re: Aliasing and subtyping structs James Kuyper <jameskuyper@verizon.net> - 2016-11-01 11:41 -0400
    Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-02 09:55 +0100
  "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-21 16:59 +0300
    Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-21 07:32 -0800
    Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-21 11:15 -0500
      Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-21 08:35 -0800
        Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-22 11:42 +0300
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 06:50 -0800
            Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-22 16:07 +0000
              Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 12:02 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-22 23:36 +0000
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 16:24 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Richard Damon <Richard@Damon-Family.org> - 2016-11-22 22:21 -0500
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-23 08:31 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] me <crisdunbar@gmail.com> - 2016-11-23 09:01 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-23 12:05 -0800
      Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-11-22 23:23 +0300
        Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-22 16:02 -0500
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 14:13 -0800
            Re: container_of() macro Noob <root@127.0.0.1> - 2016-11-23 01:10 +0100
              Re: container_of() macro <william@wilbur.25thandClement.com> - 2016-11-22 20:10 -0800
                Re: container_of() macro "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 15:10 -0800
              Re: container_of() macro David Brown <david.brown@hesbynett.no> - 2016-11-23 11:31 +0100
                Re: container_of() macro Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-23 14:07 +0300
                Re: container_of() macro supercat@casperkitty.com - 2016-11-23 08:41 -0800
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-11-30 00:14 +0300
            Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-29 17:23 -0500
              Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-04 18:48 +0300
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-04 12:17 -0500
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-04 22:15 +0300
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-04 15:06 -0500
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-12 22:31 +0300
    Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 13:39 -0800
      Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] luser droog <luser.droog@gmail.com> - 2016-11-23 15:10 -0800
        Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 15:13 -0800
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] luser droog <luser.droog@gmail.com> - 2016-11-23 18:29 -0800
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-24 11:41 +0300
          Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-24 03:29 -0800
            Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-24 17:37 -0800
              Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-25 09:55 -0800
              Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-25 15:22 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-28 18:18 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-29 04:48 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-29 07:28 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-30 08:26 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-30 09:13 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-12-12 16:44 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Melzzzzz <mel@zzzzz.com> - 2016-12-13 02:18 +0100
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 09:47 -0800
                Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 10:14 -0800

csiph-web