Size of pointer to opaque object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ellarco

    Size of pointer to opaque object

    ``Opaque-pointer representing the ID of an object.
    struct _objID;
    typedef struct _objID * objectID;''

    Hi again. Im using an api that defines an objectID type. The above
    represents the
    extent of the documentation on it.

    The above means that objectID is defined as a pointer to an as yet undefined
    class/struct _objID, and, if I understand the term correctly, is opaque in
    so far as the actual nature of the _objID class/struct is as yet unknown,
    yeah?

    My problem is that I need to statically determine the size of an objectID. I
    need to now that it occupies the same amount of space as 1,2,3... chars, but
    without using sizeof(objectID ). Is it possible? I was thinking that it might
    be the same size as char* or something convenient like that.

    TIA



  • Victor Bazarov

    #2
    Re: Size of pointer to opaque object

    "Ellarco" <nobrow@eircom. net> wrote...[color=blue]
    > ``Opaque-pointer representing the ID of an object.
    > struct _objID;
    > typedef struct _objID * objectID;''
    >
    > Hi again. Im using an api that defines an objectID type. The above
    > represents the
    > extent of the documentation on it.
    >
    > The above means that objectID is defined as a pointer to an as yet[/color]
    undefined[color=blue]
    > class/struct _objID, and, if I understand the term correctly, is opaque[/color]
    in[color=blue]
    > so far as the actual nature of the _objID class/struct is as yet unknown,
    > yeah?
    >
    > My problem is that I need to statically determine the size of an objectID.[/color]
    I[color=blue]
    > need to now that it occupies the same amount of space as 1,2,3... chars,[/color]
    but[color=blue]
    > without using sizeof(objectID ). Is it possible? I was thinking that it[/color]
    might[color=blue]
    > be the same size as char* or something convenient like that.[/color]

    The size of a pointer to any object is the same and is the same as
    a pointer to void. So, you may use sizeof(void*) for your purposes.

    Victor


    Comment

    • Gianni Mariani

      #3
      Re: Size of pointer to opaque object

      Ellarco wrote:
      ....[color=blue]
      > My problem is that I need to statically determine the size of an objectID. I
      > need to now that it occupies the same amount of space as 1,2,3... chars, but
      > without using sizeof(objectID ). Is it possible? I was thinking that it might
      > be the same size as char* or something convenient like that.[/color]

      What is stopping you from using sizeof(objectID ) ?

      Comment

      • Alexander Terekhov

        #4
        Re: Size of pointer to opaque object


        Victor Bazarov wrote:
        [...][color=blue][color=green]
        > > My problem is that I need to statically determine the size of an objectID. I
        > > need to now that it occupies the same amount of space as 1,2,3... chars, but
        > > without using sizeof(objectID ). Is it possible?[/color][/color]

        Yes. Use sizeof(_objID *).
        [color=blue][color=green]
        > > I was thinking that it might
        > > be the same size as char* or something convenient like that.[/color]
        >
        > The size of a pointer to any object is the same and is the same as
        > a pointer to void.[/color]

        Change the clinic, Bazarov.

        regards,
        alexander.

        Comment

        • Ron Natalie

          #5
          Re: Size of pointer to opaque object


          "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:L5hdb.1634 36$mp.88269@rwc rnsc51.ops.asp. att.net...
          [color=blue]
          > The size of a pointer to any object is the same and is the same as
          > a pointer to void. So, you may use sizeof(void*) for your purposes.
          >[/color]
          This is NOT true. A void* is required to hold a converted poitner value to
          any object and have the same representation as char*, but it is not necessary
          for it to be the same size as all the other pointers.

          What is true however, is that the sizeof the pointer to any class has to be
          the same size. If this were not true, than creating objects of type pointer
          to incomplete classes, wouldn't be possible as the size would not be known.
          However, this size can be smaller than sizeof(void*).


          Comment

          • WW

            #6
            Re: Size of pointer to opaque object

            Alexander Terekhov wrote:
            [SNIP][color=blue][color=green]
            >> The size of a pointer to any object is the same and is the same as
            >> a pointer to void.[/color]
            >
            > Change the clinic, Bazarov.[/color]

            Change the "cinic", Terekhov.

            BTW it can make a whole lot of improvement in communication if you also say
            want the point is, not only the insult part. :-)

            --
            Whitov Wolfov aka Attilov Feherov

            Ps: Hota moja ckraju netchevo ne znaju (try to dechipher it, I'va heard it
            from my father)


            Comment

            • Alexander Terekhov

              #7
              Re: Size of pointer to opaque object


              WW wrote:
              [...][color=blue]
              > BTW it can make a whole lot of improvement in communication if you also say
              > want the point is, not only the insult part. :-)[/color]

              What would Ron do here, then?
              [color=blue]
              >
              > --
              > Whitov Wolfov aka Attilov Feherov
              >
              > Ps: Hota moja ckraju netchevo ne znaju (try to dechipher it, I'va heard it
              > from my father)[/color]

              That was probably in reply to your mother asking (with some "elements"
              of shouting) your father "Where's The Money Gone?" (or something like
              that), I guess. ;-)

              Comment

              • WW

                #8
                Re: Size of pointer to opaque object

                Alexander Terekhov wrote:[color=blue][color=green]
                >> Whitov Wolfov aka Attilov Feherov
                >>
                >> Ps: Hota moja ckraju netchevo ne znaju (try to dechipher it, I'va
                >> heard it from my father)[/color]
                >
                > That was probably in reply to your mother asking (with some "elements"
                > of shouting) your father "Where's The Money Gone?" (or something like
                > that), I guess. ;-)[/color]

                No, actually I was supposed to tell it to the Baltic state(s) borderguards
                if they keep asking too many questions. :-)

                --
                WW aka Attila


                Comment

                • Wagner Bruna

                  #9
                  Re: Size of pointer to opaque object

                  Hello,

                  "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<L5hdb.163 436$mp.88269@rw crnsc51.ops.asp .att.net>...[color=blue]
                  > "Ellarco" <nobrow@eircom. net> wrote...[color=green]
                  > > ``Opaque-pointer representing the ID of an object.
                  > > struct _objID;
                  > > typedef struct _objID * objectID;''
                  > > (...)
                  > > Hi again. Im using an api that defines an objectID type. The above
                  > > represents the extent of the documentation on it.
                  > >
                  > > The above means that objectID is defined as a pointer to an as yet
                  > > undefined class/struct _objID, and, if I understand the term
                  > > correctly, is opaque in so far as the actual nature of the _objID
                  > > class/struct is as yet unknown, yeah?
                  > >
                  > > My problem is that I need to statically determine the size of an objectID.[/color][/color]

                  Just say 'sizeof (objectID)'.

                  To know the size of any object, you need the definition of its type.
                  But if you just want to know the pointer's size, you only need a
                  forward declaration; it works even with pointers to incomplete types.

                  (...)
                  [color=blue]
                  > The size of a pointer to any object is the same and is the same as
                  > a pointer to void.[/color]

                  This is true in most platforms, but actually it's implementation
                  dependent.


                  ++t;
                  Wagner

                  Comment

                  • glen stark

                    #10
                    Re: Size of pointer to opaque object

                    Wagner Bruna wrote:[color=blue]
                    > Hello,
                    >
                    > "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<L5hdb.163 436$mp.88269@rw crnsc51.ops.asp .att.net>...
                    >[color=green]
                    >>The size of a pointer to any object is the same and is the same as
                    >>a pointer to void.[/color]
                    >
                    >
                    > This is true in most platforms, but actually it's implementation
                    > dependent.
                    >[/color]

                    This is an interesting assertion, and makes me think I don't unserstand
                    pointers correctly? I thought a pointer contained a memory address, and
                    that memory addresses had the same size regardless of what that address
                    is... Otherwise how would it be kosher for me to declare pointers to
                    undefined Classes. And since most any pointer can be cast to a pointer
                    to void, a pointer to void would have to be the same size.

                    Can you give me a counter example where the statement 'the size of a
                    pointer to any object is the same and [its size] is the same as a
                    pointer to void' does not hold true?

                    glen stark

                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: Size of pointer to opaque object



                      glen stark wrote:[color=blue]
                      >
                      > Wagner Bruna wrote:[color=green]
                      > > Hello,
                      > >
                      > > "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<L5hdb.163 436$mp.88269@rw crnsc51.ops.asp .att.net>...
                      > >[color=darkred]
                      > >>The size of a pointer to any object is the same and is the same as
                      > >>a pointer to void.[/color]
                      > >
                      > >
                      > > This is true in most platforms, but actually it's implementation
                      > > dependent.
                      > >[/color]
                      >
                      > This is an interesting assertion, and makes me think I don't unserstand
                      > pointers correctly? I thought a pointer contained a memory address, and
                      > that memory addresses had the same size regardless of what that address
                      > is...[/color]

                      One needs to distuingish between objects of class type and objects
                      of builtin type. A character pointer may indeed have a different
                      size then a pointer to a class object.
                      [color=blue]
                      > Otherwise how would it be kosher for me to declare pointers to
                      > undefined Classes.[/color]

                      pointers to class object have the same size. Although it
                      is not mentioned explicitely in the standard, it follows
                      from eg. what you say (forward declarations would not work).
                      [color=blue]
                      > And since most any pointer can be cast to a pointer
                      > to void, a pointer to void would have to be the same size.[/color]

                      A void pointer could be larger then any other pointer. The only
                      requirement is, that any pointer can be cast to void* and back
                      without loosing information.
                      [color=blue]
                      >
                      > Can you give me a counter example where the statement 'the size of a
                      > pointer to any object is the same and [its size] is the same as a
                      > pointer to void' does not hold true?[/color]

                      Well. The standard does not require this. But AFAIK there have indeed
                      been machines where character pointers where treated differently due
                      to alignment restrictions on that CPU. Eg. if you know that an int
                      must always be stored at an address divisible by 4, then you know that
                      the last 2 bits of that address are always 0. So instead of storing
                      those 2 bits, you can shift the whole address 2 times to the right
                      and still haven't lost any information (but gained a larger address
                      room). But the same may not work for characters where you want to
                      address each and every byte.

                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Ron Natalie

                        #12
                        Re: Size of pointer to opaque object


                        "glen stark" <mail@glenstark .org.nospam> wrote in message news:3f7a903c$1 @pfaff2.ethz.ch ...
                        [color=blue]
                        >
                        > This is an interesting assertion, and makes me think I don't unserstand
                        > pointers correctly? I thought a pointer contained a memory address, and
                        > that memory addresses had the same size regardless of what that address
                        > is...[/color]

                        Your understanding is incomplete. A pointer is not a memory address. A pointer
                        is a data type that refers to a location in memory. The pointer is frequently
                        some hardware supported memory or register format. Not all machines just use
                        a 32-bit byte addressed value. Some machines have word based pointer types
                        and would to have additional information for addressing partial words which may
                        make the pointers to those types larger. The language forsees this. No where
                        in the document does it say pointers are the same size or even the same layout
                        with the exception that void* and char* are the same.
                        [color=blue]
                        > Otherwise how would it be kosher for me to declare pointers to
                        > undefined Classes. And since most any pointer can be cast to a pointer
                        > to void, a pointer to void would have to be the same size.[/color]

                        That's not true, a void pointer just has to be able to store a converted value
                        from any other pointer. The reverse is not true.
                        char x;
                        char* cp = (char*)(int*)(c har*) &x;
                        The char* to int* conversion may lose information resulting in an invalid
                        conversion.


                        Comment

                        • Jerry Coffin

                          #13
                          Re: Size of pointer to opaque object

                          In article <3f7a903c$1@pfa ff2.ethz.ch>, mail@glenstark. org.nospam
                          says...

                          [ ... ]
                          [color=blue]
                          > This is an interesting assertion, and makes me think I don't unserstand
                          > pointers correctly? I thought a pointer contained a memory address, and
                          > that memory addresses had the same size regardless of what that address
                          > is...[/color]

                          Maybe, or maybe not. First of all, it's necessary to distinguish
                          between pointers to data and pointers to functions. Pointers to data
                          come in two varieties: pointers to global functions are one sort of
                          things, and all of them are compatible with each other. Pointers to
                          member functions are entirely different (and only rarely useful, so I
                          won't got into further detail about them for the moment).

                          Pointers to data have a few requirements: a pointer to void has the same
                          representation as a pointer to char, and any other kind of pointer can
                          be converted to a pointer to void and back to its original type without
                          losing information. Though not explicitly stated, the fact that a
                          pointer to char has the same representation as a pointer to void seems
                          to imply that you should be able to do the same thing (convert pointer
                          to some other type to pointer to char and back without losing anything).

                          Other than that, most bets are off -- different sorts of pointers could
                          have different sizes and representations . For one example, consider a
                          64-bit machine with word-addressing, but not byte-addressing. A pointer
                          to char on such a machine might consist of the address of a word AND the
                          number of a byte within that word.

                          Another possibility would be a machine that provided hardware support
                          for quite a bit of memory protection -- a pointer on such a machine
                          might include not only the address of the object, but also information
                          about things like the greatest legal offset from that address, so you
                          can't accidentally dereference a pointer outside its bounds.

                          Both of these (roughly) represent real machines -- Crays are similar to
                          the first, and AS/400's the second.
                          [color=blue]
                          > Otherwise how would it be kosher for me to declare pointers to
                          > undefined Classes.[/color]

                          This can be handled a number of ways -- for one example, there might be
                          an optimized representation for a pointer under some circumstances, and
                          a less optimal version that will work under any circumstances. In this
                          case, a pointer based on a forward declaration would work, even though
                          it might be less efficient than would otherwise be the case.
                          [color=blue]
                          > And since most any pointer can be cast to a pointer
                          > to void, a pointer to void would have to be the same size.[/color]

                          Not so -- a pointer to void basically has to be AT LEAST as large as any
                          other (i.e. has to be able to hold all the information in any other) but
                          could contain MORE information than a pointer to some other type.

                          --
                          Later,
                          Jerry.

                          The universe is a figment of its own imagination.

                          Comment

                          Working...