Opaque pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ena8t8si@yahoo.com

    #16
    Re: Opaque pointers


    websnarf@gmail. com wrote:
    ena8t8si@yahoo. com wrote:
    websnarf@gmail. com wrote:
    chankl wrote:
    Can anyone explain what's an opaque pointer and how it's implemented in
    C?
    >
    The other answers given are failrly reasonable. However, I would just
    like to point out that opacity is generally focused on the undetailed
    struct, and not the fact that it has been typedefed to a pointer.
    I.e., the typedef struct List_T * List_T line is redundant and
    unnecessary (and also going to cause C++ compilers to go into fits.)
    You can simply use the expression struct List_T * instead of List_T
    with no ill effect.
    >
    I.e., the pointers are derivative of the overall idea of making opaque
    structs, and the additional typedef is not relevant to the opacity.
    Using a typedef allows client code to know less
    about a type's representation.
    >
    No, the typedef has to be exposed in the header file (or possibly
    somewhere else). But the typedef is just a pointer. This structure is
    essentially exposed even if you don't "look" at the header file.
    When I say a typedef "allows client code to know less"
    what I mean is code can be written that doesn't depend
    on the types representation. Not that it _can't_ be
    written to depend on the representation, but that
    it _can_ be written so that it _doesn't_ depend.
    That's an important property, and using typedef
    enables that.
    For example, you can say List_t x; /*... */ void * p = x; and the
    compiler doesn't complain -- so this is a kind of exposure. Or more
    minimally you can say sizeof (x) without trouble. Compare this with
    struct List_t *x, *y, and try to do this: *x = *y, or sizeof(*x) or
    sizeof(struct List_T). They both lead to compiler errors.
    That's true but irrelevant to the point I'm making.
    [...] Thus it is
    relevant to how opaque the type is. Using a
    typedef might be desirable or it might not
    be, but it is relevant.
    >
    If you were able to somehow *hide* this typedef, and have the typedef
    just magically be there, and somehow turn off its compatibility with
    other pointers and sizeof(), then I would agree with you. But you
    don't have this.
    >
    In fact if you want to truly "opaquify" a pointer, the way to do that,
    is to put it into an opaque struct (or union).
    To be technically accurate, C doesn't have opaque types.
    The standard doesn't mention them, and there is no construct
    in C that produces a type that is 'opaque' in the sense
    that the term is used in languages that have opaque types
    incorporated into the language.

    Rather, what C has is ways of making types that we
    can _think of_ as opaque. Using typedef is one way
    of doing this. Putting a struct definition in
    a source file rather than a header file is another
    way. Wrapping the representation inside a struct
    and using only the struct in client code is another
    way. Each of these ways may offer certain advantages
    or disadvantages, but none of them creates a type
    that is truly opaque, only a type that is convenient
    to think of as opaque. That's why using typedef
    is relevant to the notion of opaque types in C.

    Comment

    • websnarf@gmail.com

      #17
      Re: Opaque pointers

      ena8t8si@yahoo. com wrote:
      websnarf@gmail. com wrote:
      ena8t8si@yahoo. com wrote:
      websnarf@gmail. com wrote:
      chankl wrote:
      Can anyone explain what's an opaque pointer and how it's implemented in
      C?

      The other answers given are failrly reasonable. However, I would just
      like to point out that opacity is generally focused on the undetailed
      struct, and not the fact that it has been typedefed to a pointer.
      I.e., the typedef struct List_T * List_T line is redundant and
      unnecessary (and also going to cause C++ compilers to go into fits.)
      You can simply use the expression struct List_T * instead of List_T
      with no ill effect.

      I.e., the pointers are derivative of the overall idea of making opaque
      structs, and the additional typedef is not relevant to the opacity.
      >
      Using a typedef allows client code to know less
      about a type's representation.
      No, the typedef has to be exposed in the header file (or possibly
      somewhere else). But the typedef is just a pointer. This structure is
      essentially exposed even if you don't "look" at the header file.
      >
      When I say a typedef "allows client code to know less"
      what I mean is code can be written that doesn't depend
      on the types representation.
      But the same would be true if you used, say, a #define to try to "hide"
      the definition as well.
      [...] Not that it _can't_ be
      written to depend on the representation, but that
      it _can_ be written so that it _doesn't_ depend.
      But that's totally nonsense. If you actively avoid such direct access,
      then it doesn't matter whether you've hidden the definition or not.
      The point of using real language support for opacity is to have the
      compiler work with you in enforcing such opacity. Some might consider
      this a part of what it means to have strong typing.
      That's an important property, and using typedef
      enables that.
      Except that this doesn't distinguish itself from #defines which can
      accomplish the same thing.
      For example, you can say List_t x; /*... */ void * p = x; and the
      compiler doesn't complain -- so this is a kind of exposure. Or more
      minimally you can say sizeof (x) without trouble. Compare this with
      struct List_t *x, *y, and try to do this: *x = *y, or sizeof(*x) or
      sizeof(struct List_T). They both lead to compiler errors.
      >
      That's true but irrelevant to the point I'm making.
      I'm sure it is -- but it seems to me that its your point which is less
      relevant to the topic of real opacity.
      [...] Thus it is
      relevant to how opaque the type is. Using a
      typedef might be desirable or it might not
      be, but it is relevant.
      If you were able to somehow *hide* this typedef, and have the typedef
      just magically be there, and somehow turn off its compatibility with
      other pointers and sizeof(), then I would agree with you. But you
      don't have this.

      In fact if you want to truly "opaquify" a pointer, the way to do that,
      is to put it into an opaque struct (or union).
      >
      To be technically accurate, C doesn't have opaque types.
      The standard doesn't mention them, [...]
      Ok, now you are slipping into Keith Thompson mode. Just because the
      standard does not explicitely use the language, doesn't mean the thing
      isn't there. C *DOES* contain the following: variables, a stack and a
      heap (or dynamic memory pool), regardless of what the standard says
      (even if the implementation of the later two are not specified).
      That's because all those are general concepts that the C language has
      support for, even if not spelled out in the specification. Similarly,
      my computer has the ability to factor large integers even though none
      of its specifications explicitely say that it can do that.

      Same is true of opacity. C isn't missing opacity because it doesn't
      discuss it in the standard. Opacity is merely a *property* of certain
      declarations (structs and unions that are not fully specified and void
      * pointers). This is a real concept because of how it dictates the
      interaction between *two* programmers. If one programmer makes an
      opaque type, then another cannot normally gain access to its explicit
      definition, and this is enforced (more like 'supported' for void *'s)
      by the compiler/language. So the two programmers don't even have to
      contact each other to understand how things are supposed to be used.
      On the other hand, if one is simply relying on typedef, the other
      programmer can just read the header file see that its a typedef and
      just go ahead and do things like handle->privateEntry (unless real
      opacity via a struct or union is in there).
      [...] and there is no construct
      in C that produces a type that is 'opaque' in the sense
      that the term is used in languages that have opaque types
      incorporated into the language.
      Obviously every language puts a slightly different spin on things
      (compare tables in Lua with tables in TCL with Dictionaries in Python
      with hashes in Perl; or coroutines in Lua versus generators in Python).
      C's main weakness is that an "opaque type" can be *falsely* redefined
      anyways -- but doing so, is an explicit subversion of intent, and
      should not be a problem in real practice (assuming you are doing things
      in good faith -- i.e., avoid the void * method, and don't ever define
      any struct/union in more than one file). Other languages are likely to
      have tougher enforcement, but the main concepts are the same.
      Rather, what C has is ways of making types that we
      can _think of_ as opaque.
      If such mechanisms exist primarily in your mind, then they apply
      equally to all other languages, and exposed mechanisms from any data
      type anywhere. There is nothing special about typedef which
      corresponds to one's "thinking". If you are just one programmer, its
      not a problem at all to "think" about certain data types as being
      opaque in certain contexts regardless of what the real state is --
      typedef has nothing to do with this.

      There is a big difference between opacity enforced by a compiler and
      opacity conceptualized through convention. And this is most critically
      seen as the number of developers on a project increases.

      --
      Paul Hsieh
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



      Comment

      • ena8t8si@yahoo.com

        #18
        Re: Opaque pointers


        websnarf@gmail. com wrote:
        ena8t8si@yahoo. com wrote:
        websnarf@gmail. com wrote:
        ena8t8si@yahoo. com wrote:
        websnarf@gmail. com wrote:
        chankl wrote:
        Can anyone explain what's an opaque pointer and how it's implemented in
        C?
        >
        The other answers given are failrly reasonable. However, I would just
        like to point out that opacity is generally focused on the undetailed
        struct, and not the fact that it has been typedefed to a pointer.
        I.e., the typedef struct List_T * List_T line is redundant and
        unnecessary (and also going to cause C++ compilers to go into fits.)
        You can simply use the expression struct List_T * instead of List_T
        with no ill effect.
        >
        I.e., the pointers are derivative of the overall idea of making opaque
        structs, and the additional typedef is not relevant to the opacity.

        Using a typedef allows client code to know less
        about a type's representation.
        >
        No, the typedef has to be exposed in the header file (or possibly
        somewhere else). But the typedef is just a pointer. This structure is
        essentially exposed even if you don't "look" at the header file.
        When I say a typedef "allows client code to know less"
        what I mean is code can be written that doesn't depend
        on the types representation.
        >
        But the same would be true if you used, say, a #define to try to "hide"
        the definition as well.
        Yes, that is another technique, although using typedef
        is usually better.
        [...] Not that it _can't_ be
        written to depend on the representation, but that
        it _can_ be written so that it _doesn't_ depend.
        >
        But that's totally nonsense. If you actively avoid such direct access,
        then it doesn't matter whether you've hidden the definition or not.
        Suppose the type in question is an unsigned int. If there is no
        typedef, then client code declaring variables of the type
        would use

        unsigned xyz;

        If later we wanted to change the representation to be
        a pointer rather than an unsigned, then client code
        would need to be changed to accommodate the new type.

        If typedef had been used, no client code would need to
        be changed; the client code doesn't depend on what
        underlying type is used to represent the type name.
        The point of using real language support for opacity is to have the
        compiler work with you in enforcing such opacity. [...]
        That is one aspect of opaque types but not the only
        aspect.
        That's an important property, and using typedef
        enables that.
        >
        Except that this doesn't distinguish itself from #defines which can
        accomplish the same thing.
        Perhaps you need to review the differences between

        typedef struct foo_struct *Foo;

        and

        #define Foo struct foo_struct *

        which, although they both define Foo, cannot be
        used interchangeably .
        For example, you can say List_t x; /*... */ void * p = x; and the
        compiler doesn't complain -- so this is a kind of exposure. Or more
        minimally you can say sizeof (x) without trouble. Compare this with
        struct List_t *x, *y, and try to do this: *x = *y, or sizeof(*x) or
        sizeof(struct List_T). They both lead to compiler errors.
        That's true but irrelevant to the point I'm making.
        >
        I'm sure it is -- but it seems to me that its your point which is less
        relevant to the topic of real opacity.
        You have a narrow view of what it means to program
        using "opaque types". You're welcome to your view,
        but it isn't the only view.
        [...] Thus it is
        relevant to how opaque the type is. Using a
        typedef might be desirable or it might not
        be, but it is relevant.
        >
        If you were able to somehow *hide* this typedef, and have the typedef
        just magically be there, and somehow turn off its compatibility with
        other pointers and sizeof(), then I would agree with you. But you
        don't have this.
        >
        In fact if you want to truly "opaquify" a pointer, the way to do that,
        is to put it into an opaque struct (or union).
        To be technically accurate, C doesn't have opaque types.
        The standard doesn't mention them, [...]
        >
        Ok, now you are slipping into Keith Thompson mode. [...]
        Oh, a compliment - thank you! Would I be returning the
        favor if I said you were slipping into websnarf mode?
        Rather, what C has is ways of making types that we
        can _think of_ as opaque.
        >
        If such mechanisms exist primarily in your mind, then they apply
        equally to all other languages, and exposed mechanisms from any data
        type anywhere. There is nothing special about typedef which
        corresponds to one's "thinking". [...]
        If you say there isn't for you, I have no problem
        with that. Not everyone shares your viewpoint, however.
        There is a big difference between opacity enforced by a compiler and
        opacity conceptualized through convention. And this is most critically
        seen as the number of developers on a project increases.
        I wasn't arguing the relative merits, only saying that
        using typedef is relevant to the notion of opaque types
        in C. Using typedef facilitates one kind of advantages;
        using hidden struct definitions facilitates another kind
        of advantages. Both kinds relate to opaque types.

        Comment

        • Dave Thompson

          #19
          Re: Opaque pointers

          On Sat, 19 Aug 2006 10:01:35 -0400, CBFalconer <cbfalconer@yah oo.com>
          wrote:
          <snip>
          Your last comment is about the only sensible thing in this whole
          thread. The point about opaque pointers is that they allow
          complete hiding of the implementation, thus preserving the freedom
          to modify without harming the usability. Let's take an example:
          >
          /* foolib.h contains */
          typedef struct foo *fooptr;
          >
          fooptr fooinit(/* various params */);
          <snip etc.>
          This has various advantages. For one, the library can be fully
          re-entrant, provided the passed in fooids are distinct. Thus no
          extra code is needed for multiple instances. The library
          That is not an advantage for this technique; it is equally possible
          with nonopaque types. In both cases, as you indicate later, it
          requires some care, and is not an inherent property.
          maintainer is completely free to revise the code (but not
          foolib.h), algorithms, etc. used without affecting the user code in
          In theory. In practice, at least if foolib is substantial in
          functionality and significantly used, at least some clients tend to
          become dependent on properties additional to those in the (rather
          limited) C specification of the interface -- and get very upset if
          broken by an implementation change. However, even being
          _substantially_ free to change the library without _any_ change to
          clients is A Good Thing (tm) and usually Worth It (tm).

          Aside: who was it used to trademark words like this? I've forgotten.
          any way. There is no way for a user to affect the internals of the
          foolib, other than by random chance probing into a struct foo, so
          the library integrity is assured (as far as possible with the C
          language.) <snip>
          Agree, with the sadly-too-important qualification you gave, plus
          another: if foolib depends on some process (or wider) resource(s) also
          accessible to the client, or for that matter barlib. Obvious and
          too-common examples are things like: using up memory; (mis)using fds
          or other capabilities like sockets; changing the locale, or working
          directory, or alarm timer(s). But again, pretty-good isolation well
          short of perfect is still a good deal better than nothing.


          - David.Thompson1 at worldnet.att.ne t

          Comment

          Working...