home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / cplus / 2019 < prev    next >
Encoding:
Text File  |  1993-01-09  |  7.1 KB  |  173 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Use of nested functions (Was: Proposal for default scope)
  5. Message-ID: <1993Jan9.182938.16874@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <9300817.11209@mulga.cs.mu.OZ.AU> <1993Jan8.192906.2342@qualcomm.com>
  10. Date: Sat, 9 Jan 1993 18:29:38 GMT
  11. Lines: 160
  12.  
  13. In article <1993Jan8.192906.2342@qualcomm.com> greg@qualcom.qualcomm.com (Greg Noel) writes:
  14. >Fergus James HENDERSON <fjh@munta.cs.mu.OZ.AU> writes:
  15. >>Many people seem to think that the purpose of nested functions is purely
  16. >>as encapsulation, a sort of code organization/modularization feature.
  17. >>... the real reason that they are useful is that ... their addresses can
  18. >>be passed to any function that just expects a function parameter.
  19. >>If a local function never has its address taken, then it does not use
  20. >>the full power of nested functions.
  21. >
  22. >This is a valid point, but this is exactly the part that is expensive to
  23. >implement.  
  24.  
  25.     Not necessarily.
  26.  
  27. >The ``Spirit of C'' is that expensive things are exposed to
  28. >the programmer; C++ inherits much of this spirit.
  29.  
  30.     Not entirely. Building objects can be VERY expensive.
  31. Have a look at some non-optimised code for a constructor of
  32. a complex object sometime.
  33.  
  34.     And the flip side: C traditionally provides facilities
  35. corresponding to the underlying hardware. Many CISC machines,
  36. including the 386 and 68000 have instructions and architecture
  37. *specifically* designed to support nested functions.
  38.  
  39.     Thus NOT providing nested functions is against the spirit of C.
  40.  
  41. >
  42. >If you look at what a compiler must do to implement this, it has to create
  43. >a ``structure'' (the stack frame) that contains all the local variables,
  44. >the parameters, and so forth, and arrange for the nested function to be
  45. >able to find that structure with a sort of implicit ``this'' pointer.  
  46.  
  47.     What happens on a normal call is trivial. There is a second
  48. stack called a 'display' which maintains a pointer to each 
  49. accessible activation record. On the 386 the display is kept
  50. on the stack, and copied each time. It is also possible
  51. to just allocate 'n' words of memory somewhere (for 'n' levels
  52. of nesting) and move the appropriate pointers in and out
  53. as the level changes.
  54.  
  55. >In
  56. >a heavily optimizing compiler that keeps many values in registers, it can
  57. >be costly to have to re-sync memory every time a nested function _might_
  58. >be invoked.
  59.  
  60.     Dont understand '_might_'. Either one is invoked or not.
  61. >
  62. >On top of that, if you wish a function to retain its scope during a potentially
  63. >recursive invocation, it means that the implicit ``this'' pointer must be
  64. >kept as a part of the pointer, which would increase the size of _every_
  65. >pointer, whether the feature is used or not.  That was a real killer for
  66. >PL/I function pointers and very much against the C philosophy.
  67.  
  68.     The C memory model is bogus. It is based on the outmoded
  69. von Neumann machine. ANY function accessing global data that
  70. has to be re-entrant MUST be passes an instance-specific
  71. data pointer anyhow. In other words: normal functions ALREADY
  72. require BOTH a code address and a data pointer to be
  73. invokable. (Pure functions dont though).
  74.  
  75.     SO: if you are using a linear address machine,
  76. you can use trampolines and preserve function pointers
  77. as one pointer for nested functions.
  78.  
  79.     IF you use a real machine with segments and sharable code,
  80. you already needed two pointers ANYHOW, so you loose nothing.
  81.  
  82. >
  83. >(BTW, I'd like to have nested functions, but I would not make pointers to
  84. >them be compatible with a ``normal'' function pointer.  
  85.  
  86.     Indeed this is an option. Can we discuss the options
  87. for function pointers further? Seems to me this is the only
  88. serious technical problem in writing a proposal.
  89.  
  90.     Do we require nested functions to have the same form
  91. as ordinary ones?
  92.  
  93.     If not, how are they to be distinguished?
  94.  
  95. >Nested functions
  96. >are handy to improve the clarity of code, but I'd want the compiler to
  97. >have total knowledge over when they were invoked and allow it to optimize
  98. >them as it saw fit, so that it could inline them or use special lightweight
  99. >calling sequences if it wished.  
  100.  
  101.     IF nested functions are defined as in standard Pascal
  102. then only forward declarations will cause a problem: that is,
  103. the compiler can inline them as it sees fit.
  104.  
  105.     IF a nested function is called through a pointer,
  106. it isnt easy to see how much optimisation could be done:
  107. the call could be made from a completely different module,
  108. i.e. a separate translation unit.
  109.  
  110. >If a pointer syntax is needed, I'd make
  111. >it similar to the C++ class pointer notation, using the function name as
  112. >the class---after all, that's really what's going on....)
  113.  
  114.     I think .. not sure .. it is only necessary to
  115. know whether the pointer is a nested function pointer,
  116. a pure function pointer (special case---no environmental data),
  117. or a rubbish C function (can access the 'global' activation
  118. record which can be done without the data pointer if you
  119. are prepared to give up re-entrancy)
  120.  
  121.     Come to think of it, one could easily
  122. take the address of a object member function and execute that
  123. indirectly too--- it is exactly the function address plus
  124. the objects 'this' pointer.
  125.  
  126.     This is not surprising---nested functions ARE member
  127. functions of the objects which are the activation records
  128. of the scopes in which they are declared <pant>.
  129. If we had garbage collection, we wouldn't need to bother with 
  130. classes, we could use closures instead.
  131.  
  132.     However, having made the observation, could we kill two
  133. birds with one stone and all a new type of function pointer
  134. that can execute EITHER a nested function OR a member
  135. function of a particular object?
  136.  
  137. >
  138. >But, as others have pointed out, it's possible for you, the programmer, to
  139. >do the expensive thing 
  140.  
  141.     Yes but VERY clumbsily :-)
  142.  
  143. >that the compiler would have to do, 
  144.  
  145.     NO the compiler does NOT have to do it expensively for
  146. an ordinary call. On the contrary, it can be HIGHLY optimal.
  147. As YOU pointed out yourself :-)
  148.  
  149. >by putting the
  150. >variables you need in a structure and keeping a pointer to it where the
  151. >helper function can find it.  I won't claim that it's fun to do it this
  152. >way, but this is essentially what the compiler would have to generate, so
  153. >the overhead would be the same.
  154.  
  155.     You are definitely wrong here. I as a C programmer, cannot
  156. invoke the machine code instructions designed to manage
  157. nested functions. But I CAN do this in Pascal. So C is
  158. definitely slower and less efficient than Pascal here.
  159.  
  160.     And as YOU pointed out: for nested functions invoked
  161. directly, and for which the definition has been seen, 
  162. the compiler can and often would be able to inline them.
  163.  
  164.     For me to explicitly keep track of the display is sure
  165. to be slower --- even on a machine without hardware support
  166. for nested functions --- than proper compiler generated code.
  167.  
  168. -- 
  169. ;----------------------------------------------------------------------
  170.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  171.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  172. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  173.