How do you track type information when converting int* to void*

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

    How do you track type information when converting int* to void*

    Keith made the following comment

    " You can convert an int*, or an int**, or an int***, or ... to void*
    without loss of information -- except that you lose the type
    information, which you have to carefully keep track of yourself. "

    How would you track the type infornation?
  • Nick Keighley

    #2
    Re: How do you track type information when converting int* to void*

    On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
    Keith made the following comment
    " You can convert an int*, or an int**, or an int***, or ... to void*
    without loss of information -- except that you lose the type
    information, which you have to carefully keep track of yourself. "
    >
    How would you track the type infornation?
    why are you casting a whatever-type to a void*?
    If you need the type information why not keep the original type?

    I suppose some sort of generic container might be a reason
    to do this.

    Basically you need some sort of tag

    enum Tag {INT, FLOAT, STRING};

    struct Node
    {
    enum Tag tag;
    void *data;
    };

    store data like this

    struct Node nod;
    int i;
    nod.tag = INT;
    nod.data = &i;

    add (&handle, &nod);

    casting it back when you retrieve it.

    You might consider a union instead. Or a redesign.


    --
    Nick Keighley






    Comment

    • Richard Heathfield

      #3
      Re: How do you track type information when converting int* to void*

      Nick Keighley said:
      On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
      >Keith made the following comment
      >
      >" You can convert an int*, or an int**, or an int***, or ... to void*
      >without loss of information -- except that you lose the type
      >information, which you have to carefully keep track of yourself. "
      >>
      >How would you track the type infornation?
      >
      why are you casting a whatever-type to a void*?
      Where did he say he was casting?
      If you need the type information why not keep the original type?
      Because you're writing generic code?
      I suppose some sort of generic container might be a reason
      to do this.
      Ha - yes, quite so.
      Basically you need some sort of tag
      >
      enum Tag {INT, FLOAT, STRING};
      or a function whose address you hand to the container, and which knows how
      to deal with the type.
      >
      struct Node
      {
      enum Tag tag;
      void *data;
      };
      >
      store data like this
      >
      struct Node nod;
      int i;
      nod.tag = INT;
      nod.data = &i;
      >
      add (&handle, &nod);
      >
      casting it back when you retrieve it.
      Why?

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • blargg

        #4
        Re: How do you track type information when converting int* to void*

        In article
        <83dc44e7-78b6-4742-8550-c73c8c6650bc@l4 3g2000hsh.googl egroups.com>,
        Chad <cdalten@gmail. comwrote:
        Keith made the following comment
        >
        " You can convert an int*, or an int**, or an int***, or ... to void*
        without loss of information -- except that you lose the type
        information, which you have to carefully keep track of yourself. "
        >
        How would you track the type infornation?
        In many cases you know the type that the void* points to, so you can
        just cast back to that. Why use void* at all, you ask? Often a function
        doesn't need to know anything about the type pointed to, so using void*
        allows one function to work on many types. The best example is probably
        qsort(), where the comparison function gets a void*.

        Comment

        • Eric Sosman

          #5
          Re: How do you track type information when converting int* to void*

          blargg wrote:
          In article
          <83dc44e7-78b6-4742-8550-c73c8c6650bc@l4 3g2000hsh.googl egroups.com>,
          Chad <cdalten@gmail. comwrote:
          >
          >Keith made the following comment
          >>
          >" You can convert an int*, or an int**, or an int***, or ... to void*
          >without loss of information -- except that you lose the type
          >information, which you have to carefully keep track of yourself. "
          >>
          >How would you track the type infornation?
          >
          In many cases you know the type that the void* points to, so you can
          just cast back to that. Why use void* at all, you ask? Often a function
          doesn't need to know anything about the type pointed to, so using void*
          allows one function to work on many types. The best example is probably
          qsort(), where the comparison function gets a void*.
          ... and where the true type of what the void* points to
          is "tracked" by the comparison function: The caller supplies
          a pointer to a function that "knows" what's on the other end
          of the pointer, and thus specifies its type.

          One other aspect of the type, its size, is provided to
          qsort() as an explicit argument.

          --
          Eric Sosman
          esosman@ieee-dot-org.invalid

          Comment

          • Nick Keighley

            #6
            Re: How do you track type information when converting int* to void*

            On 18 Sep, 13:01, Richard Heathfield <r...@see.sig.i nvalidwrote:
            Nick Keighleysaid:
            On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
            Keith made the following comment
            " You can convert an int*, or an int**, or an int***, or ... to void*
            without loss of information -- except that you lose the type
            information, which you have to carefully keep track of yourself. "
            >
            How would you track the type infornation?
            >
            why are you casting a whatever-type to a void*?
            >
            Where did he say he was casting?
            a moment of insanity on my part

            If you need the type information why not keep the original type?
            >
            Because you're writing generic code?
            ok, but does he need generic code? Sometimes things
            get stuffed into void* for no good reason that I can see.
            But I should assume Good Faith; that the OP is not in fact
            crazy.

            I suppose some sort of generic container might be a reason
            to do this.
            >
            Ha - yes, quite so.
            >
            Basically you need some sort of tag
            casting it back when you retrieve it.
            >
            Why?
            a *second* moment of insanity


            --
            Nick Keighley

            "Almost every species in the universe has an irrational fear of the
            dark.
            But they're wrong- cos it's not irrational. It's Vashta Nerada."
            The Doctor

            Comment

            Working...