home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!hri.com!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!news.funet.fi!funic!nntp.hut.fi!nntp!sja
- From: sja@snakemail.hut.fi (Sakari Jalovaara)
- Newsgroups: comp.lang.c
- Subject: Re: Is that ansi ?
- Message-ID: <SJA.93Jan9191748@lk-hp-12.hut.fi>
- Date: 9 Jan 93 17:17:48 GMT
- References: <mcdonald.28@aries.scs.uiuc.edu> <1993Jan8.165451.7873@wraxall.inmos.co.uk>
- Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
- Organization: Helsinki University of Technology, Finland
- Lines: 43
- In-Reply-To: nathan@inmos.co.uk's message of Fri, 8 Jan 93 16:54:50 GMT
- Nntp-Posting-Host: lk-hp-12.hut.fi
-
-
- >> [pointer to function isn't guaranteed to be castable to void*]
-
- > : I'm afraid it's true, and one of the most serious faults of ANSI C.
- > And a very good reason for it too. As I emailed to Daniel, some systems require
- > more than an instruction pointer to reference a function.
-
- Indeed, for example, on an IBM RISC System/6000 function pointers SORT OF
- are 8 (or 12?) bytes. A function pointer requires a pointer to the first
- instruction and extra space for the dynamic run-time linker.
-
- BUT in reality, on a System/6000 sizeof(int (*)(void) == 4.
-
- Here is how it is done:
-
- For every function a programmer defines in a C file, the compiler adds an
- extra record like
-
- __INDIRECT_main: .long _main # This is the address of the code
- .long 0 # This is for the linker
- .long 0 # As is this
-
- When an address of a function is passed around in C, the __INDIRECT_main
- address is used - a perfectly ordinary 4-byte pointer. When a function
- is called via pointer, the compiler uses code that digs the code address
- (_main) via a double dereferencing and jumps there.
-
- (If you insist on putting function pointers in void pointers you can
- even emulate this trick manually -- hint: pointer-to-pointer-to-function
- is guaranteed to fit in void*.)
-
- I can think of two reasons why the C standard doesn't require that
- function pointers can be cast to void pointers and back:
-
- 1) The committee didn't happen to think of the above trick.
-
- 2) The committee wanted to preserve existing compilers and
- linkers on systems whose designers didn't happen to
- think of the above trick.
-
- Whether either reason is a good one or whether this is a "serious
- fault of ANSI C" can be discussed 'til the pigs come flying home.
- ++sja
-