Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!apple!apple.com!desnoyer From: desnoyer@apple.com (Peter Desnoyers) Newsgroups: comp.lang.c Subject: Re: Set function address into absolute memory? Message-ID: <2685@internal.Apple.COM> Date: 7 Jul 89 17:06:34 GMT References: <2954@helios.ee.lbl.gov> Sender: usenet@Apple.COM Distribution: usa Organization: Apple Computer, Inc. Lines: 40 In article <2954@helios.ee.lbl.gov> mikec@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