casting void pointer to be a pointer to a function 
Author Message
 casting void pointer to be a pointer to a function


|>writes:

|>>Yes, this is the problem. void* is big enough to point to any `object'
|>>whatsoever: but it is not necessarily big enough to point to a function.

|>This is new to me, so I picked my C-Bible (K&H) and read "Pointer to
|>functions". There I find: "...Every pointer can be transformed to void *
|>and vice versa without loosing information ..." (This is a re-translated
|>sentence from the german version).
|>I think this is logical. Otherwise there would be no possibilty to declare
|>library functions like qsort().

|Just be sure you're not mixing near and far pointers....

Please reread what Tanmoy wrote earlier before posting such nonsense.
Let me rephase things a bit: There are three possible sorts of types:

- object types (stuff that describes objects);
- function types (stuff that describes functions);
- incomplete types (stuff that incompletely describes objects,
  in particular: the size of the object cannot be determined from
  an incomplete type).

A void pointer can contain a pointer to an object or a pointer to
an incomplete type. That's all there is to it. Void pointers cannot
contain a pointer to a function type, no matter whether or not they're
small, near, far, big, huge, humonguous or bent.

kind regards,


--
Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!



Mon, 18 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:


>>But there is a more serious problem ...

>>Yes, this is the problem. void* is big enough to point to any `object'
>>whatsoever: but it is not necessarily big enough to point to a function.
>This is new to me, so I picked my C-Bible (K&H) and read "Pointer to functions".
>There I find: "...Every pointer can be transformed to void * and vice versa without
>loosing information ..." (This is a re-translated sentence from the german version).
>I think this is logical. Otherwise there would be no possibilty to declare library
>functions like qsort().
>Jens

Just be sure you're not mixing near and far pointers....
                        ...Woe betides...


Mon, 18 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:


>>A void pointer can contain a pointer to an object or a pointer to
>>an incomplete type. That's all there is to it. Void pointers cannot
>>contain a pointer to a function type, no matter whether or not they're
>>small, near, far, big, huge, humonguous or bent.
>The wonderful thing about C is with a pointer you can be
>anywhere and just about anything, provided you have its
>address.  I would like to disagree with Jos because I
>use void pointers to hide all sorts of evil things that
>I am doing from the compiler.  Here is a recent piece
>of C code which shows how a function jump table was
>implemented using an array of void pointers.

Unfortunately, he's right, and the code is not legal.  You could use
an array of
        void (*)(void *, int)
to do this legally, but the use of a (void *) to represent a ptr-to-function
is not legal or portable, and will fail on some machines.

Even if it worked on all known machines, it's not legal.

I can't see what this obsession is with doing things which aren't legal,
especially when there's a perfectly legal way to do them.

-s
--

C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!

The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq



Tue, 19 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:
>A void pointer can contain a pointer to an object or a pointer to
>an incomplete type. That's all there is to it. Void pointers cannot
>contain a pointer to a function type, no matter whether or not they're
>small, near, far, big, huge, humonguous or bent.

The wonderful thing about C is with a pointer you can be
anywhere and just about anything, provided you have its
address.  I would like to disagree with Jos because I
use void pointers to hide all sorts of evil things that
I am doing from the compiler.  Here is a recent piece
of C code which shows how a function jump table was
implemented using an array of void pointers.

void handler1(void *data, int count)
{

Quote:
}

void handler2(void *data, int count)
{

Quote:
}

void handler3(void *data, int count)
{

Quote:
}

void move_data(int type, void *data, int count)
{
        static void *jump_table[] =
        {
                (void *)handler1,
                (void *)handler2,
                (void *)handler3
        };

        (void (*)(void *, int))jump_table[type])(data, count);

Quote:
}



Tue, 19 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:
>>A void pointer can contain a pointer to an object or a pointer to
>>an incomplete type. That's all there is to it. Void pointers cannot
>>contain a pointer to a function type, no matter whether or not they're
>>small, near, far, big, huge, humonguous or bent.
>The wonderful thing about C is with a pointer you can be
>anywhere and just about anything, provided you have its
>address.  I would like to disagree with Jos because I
>use void pointers to hide all sorts of evil things that
>I am doing from the compiler.  

There is no guarantee whatever that a pointer to void will fit in
a pointer to function type, or vice versa.  (This is a real,
practical problem that WILL bite you sooner or later even on
"reasonable" machines).  On DOS compilers, there are middle and
compact models, where data pointers and function pointers are
not the same size.  Either one may be bigger.

Quote:
>Here is a recent piece of
very unportable
>C code which shows how a function jump table was
>implemented using an array of void pointers.

It would be much more portable if you used an array of
function pointers.

                                        Gordon L. Burditt
                                        sneaky.lonestar.org!gordon



Fri, 22 May 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Casting function pointer to void pointer

2. casting void pointer to be a pointer to a function

3. Casting function pointer to void*

4. Q: casting to a pointer to a function returning void

5. casting of struct element void pointer casting

6. pointers to functions and pointers to void.

7. Can void-pointers be casted to function-pointers?

8. How to use (pointer to function), and function and pointer to (pointer to function)

9. Casting a method pointer to a function pointer.

10. Pointer Functions and Pointers to Pointer Functions

11. Casting pinning pointer to void *

12. Casting from a void pointer

 

 
Powered by phpBB® Forum Software