Re: void pointers & void function pointers





Peter Goddard wrote:
> Hello,
> Is it possible, using casting to promote...
>
> void *Ptr;
>
> To behave like...
>
> void (*FnPtr)(void);
>
> ?
>
> i.e. Can the 'void *Ptr' be cast into calling a function once it points to
> one?

No, or "not portably." Function pointers can only point
to functions; data pointers can only point to data. If you
try to coerce a pointer of one class to a pointer of the other,
all bets are off -- on some implementations it will work as
you perhaps hoped, while on others you will be unpleasantly
surprised.

(Usually, people who want to make this conversion have
just filled an array with instructions and want to get the
program to execute them. Quite aside from the difficulties
of converting the pointer, on many machines you will face
other obstacles. For example, a machine that "knows" the
I- and D-spaces are distinct may well have separate cache
mechanisms, and may not bother to synchronize them: you may
wind up executing the garbage that occupied the array before
you stored the instructions, which are still sitting in a
data cache and have not yet been flushed back to memory.
Machines that maintain "permission bits" on regions of memory
may not set the "executable" attribute on any data memory you
can access; you'll get some kind of trap when the CPU tries to
fetch in instruction from a non-executable memory area. And
so on, and so on, into endless machine-dependent territory --
and, perhaps, an appreciation of why the C Standard chose not
to try to enter this particular bramble bush.)

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: trying to make my "fillvalues" function work...
    ... void checkPtr_error(const char *location, const void *pointer) ... fprintf(stderr, "Memory allocation failure (%s). ...
    (comp.lang.c)
  • Re: sizeof(ptr) = ?
    ... The value returned by malloc() is of type 'void*', ... The memory is typeless until an object has been written ... Since 'void' is defined to be an incomplete ... an lvalue of a complete type, there must be a pointer conversion ...
    (comp.lang.c)
  • Re: allocating mem in a function and assigning a ptr to the first byte of that mem array...
    ... Anway the idea is that there's a func Test which takes a pointer to ... void argument, allocate some memory, set this memory with some cotent, ... int main(int argc, char **argv) ...
    (comp.lang.c)
  • Re: The usage of %p in C
    ... Can you explain how,say, a "special" pointer can be ... stored in a malloc'ed block of memory which basically returns a single ... as long as converting from one to the other and back works. ... instead of the void * it has been told to expect, and what's worse, it ...
    (comp.lang.c)
  • Re: alligned memory allocation
    ... > I ve got the following piece of code which does the role of allocating ... > aligned memory. ... It is a case of a clueless programmer trying to return a pointer that is ... of a void* value. ...
    (comp.lang.c)