Arcane C hacks?
Chris Torek
chris at umcp-cs.UUCP
Thu Feb 27 11:40:47 AEST 1986
In article <1067 at houxf.UUCP> jph at houxf.UUCP (Jack Harkins)
. . . responds to the following from Barry Shein:
>>declare a table something like: int (*funtable[MAXFNS])() ;
>>and just malloc the storage for the generated code. ... It should be
>>quite portable (code generator aside) and is legal C.
>
>Well, some compilers will dislike the attempt to cast a (char *) into
>a (int (*)()) ; in fact I think some will call it an outright error
Jack Harkins says:
>This line of code DOES NOT cast a char * into an int. It is declaring
>that funtable is an array of MAXFNS elements, each of which is a pointer
>to a function that returns a int value.
You are both right. It is obvious that the line Jack refers to is:
int (*funtable[MAXFNS])();
while the code Barry refers to is:
char *malloc();
funtable[n] = (int (*)()) malloc(codesize);
(which does not appear in the quoted text, but is implied nonetheless.)
So when Barry says:
> it is _not_ portable to the 3b2,
he is correct: you cannot invoke the allocated function without
turning it into `code' first, for the hardware will not execute
`data'; and when Jack says:
>I have programs that use pointers to functions, some that run on 3B2/5's.
>The construct is totally(no flames, please) portable.
he is also correct: pointers to functions are portable. It is this
specific usage---allocate data, fill with code, call data area as
function---that is not.
Hoping this will forestall further confusion,
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP: seismo!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list