Help on void *

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

    #1

    Help on void *

    Hello,

    Does any body know the advantage of writing function argument as void*
    (rather than using int*, char* etc). what is the advantage or
    disadvantage of this.
    I've seen most of the library functions are implemented this was, as
    they provide generic interface.

    /*Function defination*/

    int Msg_Edit(void *ptr)
    {

    return *((int*)ptr); //here i should know the type before hand as
    int....


    }


    /* Function call */

    Msg_Edit(&msg);


    Now msg buffer can be of any type char, int, float....(Am i right)

    Is there a way in way in C, by which i know the type of pointer passed
    to the function on runtime.
    otherwise i'll need to typecast ptr to a specific type (int*, char*
    etc) which should be prior knownto me.

    Then, what is the main advantage of this approach...

    Best Regards,

    #define.

  • Eric Sosman

    #2
    Re: Help on void *



    #define wrote On 12/19/05 10:33,:[color=blue]
    > Hello,
    >
    > Does any body know the advantage of writing function argument as void*
    > (rather than using int*, char* etc). what is the advantage or
    > disadvantage of this.[/color]

    Disadvantage: A `void*' carries no information about
    the actual type of the data it points to, so the function
    needs to obtain that information in some other way.

    Advantage: A `void*' carries no information about the
    actual type of the data it points to, so a function that
    doesn't need to know the type isn't burdened with trying
    to figure it out.
    [color=blue]
    > I've seen most of the library functions are implemented this was, as
    > they provide generic interface.[/color]

    Exactly. memcpy() doesn't need to know the type of
    the data it's copying, so it can use `void*'. Without a
    "generic" pointer of some kind, you'd need separate versions
    of memcpy() for copying characters, shorts, ints, floats,
    doubles, ... And, of course, for all the kinds of types a
    programmer might invent: struct foo, union bar, ...

    On the other hand, some functions *do* require specific
    types, and they don't use `void*'. For example, strcpy()
    is only for copying character strings -- not arrays of
    `unsigned long', for example -- so it uses `char*' pointers.
    [color=blue]
    > /*Function defination*/
    >
    > int Msg_Edit(void *ptr)
    > {
    >
    > return *((int*)ptr); //here i should know the type before hand as
    > int....
    >
    >
    > }
    >
    >
    > /* Function call */
    >
    > Msg_Edit(&msg);
    >
    >
    > Now msg buffer can be of any type char, int, float....(Am i right)[/color]

    Yes.
    [color=blue]
    > Is there a way in way in C, by which i know the type of pointer passed
    > to the function on runtime.[/color]

    There is nothing built-in. If you need to know the type
    of some piece of data, you need to keep track of it yourself.
    [color=blue]
    > otherwise i'll need to typecast ptr to a specific type (int*, char*
    > etc) which should be prior knownto me.
    >
    > Then, what is the main advantage of this approach...
    >
    > Best Regards,
    >
    > #define.
    >[/color]

    --
    Eric.Sosman@sun .com

    Comment

    • dukguru

      #3
      Re: Help on void *

      in c++,
      void FuncA(void *p)
      {
      if(typeid(*p) == typeid(int)) // RTTI check
      {
      // p is int pointer
      }
      }

      // #include <typeinfo>

      no way in c

      ==
      dukguru@gmail.c om

      Comment

      • dukguru

        #4
        Re: Help on void *

        in C++

        you can check RTTI



        no way in C

        Comment

        • Dave Thompson

          #5
          Re: Help on void *

          On 20 Dec 2005 18:33:53 -0800, "dukguru" <dukguru@gmail. com> wrote:
          [color=blue]
          > in c++,
          > void FuncA(void *p)
          > {
          > if(typeid(*p) == typeid(int)) // RTTI check
          > {
          > // p is int pointer
          > }
          > }
          >
          > // #include <typeinfo>
          >[/color]
          <OT> No, (even) in C++ the runtime/dynamic part of RTTI only works for
          classes with virtual methods; 5.2.8[expr.typeid] 10.3[class.virtual].
          typeid(*voidptr ) is statically 'void' and never == that of int.

          IIRC Icon does support type inquiry on all types, and maybe Eiffel.
          And LISP, of course. <G>

          - David.Thompson1 at worldnet.att.ne t

          Comment

          Working...