Set function address into absolute memory?

Peter Desnoyers desnoyer at apple.com
Sat Jul 8 03:06:34 AEST 1989


In article <2954 at helios.ee.lbl.gov> mikec at ux1.lbl.gov (Mike Chin) writes:
> Is there a reasonable way to place the address of a function into
> an absolute location? I would have thought that something like "a pointer
> to a function"
> set to the table address would work [...]

It should. See below.

> void main(){
>     void    (*funcpoint)(); 
                   ^
       i.e. (*funcpoint)() is void, and *funcpoint is a function -
       therefore the value of funcpoint is the address of the first
       instruction in that function.
>
>         funcpoint=(void*) malloc (4); 
                       ^
         Allocate memory to put the code into??? and then cast it
         to type void, instead of void (*)()? (need a typedef to do
         this cast correctly, although there's not much point to it)

[more code...]

What you want to do is allocate an array of pointers to functions
(the malloc'ed pointer will be type pointer to pointer to function),
put a pointer to a function in the space so allocated, and proceed
from there. e.g. -

  /* code fragment: */
  typedef void (*fptr)();   /* type fptr is "pointer to function" */
  fptr * tmp;               /* tmp points to an array of fptrs */
  tmp = (fptr *) malloc( sizeof( fptr));  /* allocate 1 fptr */
  tmp[0] = vector;          /* and set the value */
  (*tmp[0])();              /* now call vector() */

This lints with no errors. 

                                      Peter Desnoyers
                                      Apple ATG
                                      (408) 974-4469



More information about the Comp.lang.c mailing list