home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19477 < prev    next >
Encoding:
Internet Message Format  |  1993-01-09  |  2.3 KB

  1. 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
  2. From: sja@snakemail.hut.fi (Sakari Jalovaara)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is that ansi ?
  5. Message-ID: <SJA.93Jan9191748@lk-hp-12.hut.fi>
  6. Date: 9 Jan 93 17:17:48 GMT
  7. References: <mcdonald.28@aries.scs.uiuc.edu> <1993Jan8.165451.7873@wraxall.inmos.co.uk>
  8. Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
  9. Organization: Helsinki University of Technology, Finland
  10. Lines: 43
  11. In-Reply-To: nathan@inmos.co.uk's message of Fri, 8 Jan 93 16:54:50 GMT
  12. Nntp-Posting-Host: lk-hp-12.hut.fi
  13.  
  14.  
  15. >> [pointer to function isn't guaranteed to be castable to void*]
  16.  
  17. > : I'm afraid it's true, and one of the most serious faults of ANSI C.
  18. > And a very good reason for it too. As I emailed to Daniel, some systems require
  19. > more than an instruction pointer to reference a function.
  20.  
  21. Indeed, for example, on an IBM RISC System/6000 function pointers SORT OF
  22. are 8 (or 12?) bytes.  A function pointer requires a pointer to the first
  23. instruction and extra space for the dynamic run-time linker.
  24.  
  25. BUT in reality, on a System/6000   sizeof(int (*)(void) == 4.
  26.  
  27. Here is how it is done:
  28.  
  29. For every function a programmer defines in a C file, the compiler adds an
  30. extra record like
  31.  
  32.     __INDIRECT_main:    .long    _main    # This is the address of the code
  33.             .long    0    # This is for the linker
  34.             .long    0    # As is this
  35.  
  36. When an address of a function is passed around in C, the __INDIRECT_main
  37. address is used - a perfectly ordinary 4-byte pointer.  When a function
  38. is called via pointer, the compiler uses code that digs the code address
  39. (_main) via a double dereferencing and jumps there.
  40.  
  41. (If you insist on putting function pointers in void pointers you can
  42. even emulate this trick manually -- hint: pointer-to-pointer-to-function
  43. is guaranteed to fit in void*.)
  44.  
  45. I can think of two reasons why the C standard doesn't require that
  46. function pointers can be cast to void pointers and back:
  47.  
  48.     1) The committee didn't happen to think of the above trick.
  49.  
  50.     2) The committee wanted to preserve existing compilers and
  51.        linkers on systems whose designers didn't happen to
  52.        think of the above trick.
  53.  
  54. Whether either reason is a good one or whether this is a "serious
  55. fault of ANSI C" can be discussed 'til the pigs come flying home.
  56.                                     ++sja
  57.