home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19135 < prev    next >
Encoding:
Internet Message Format  |  1993-01-02  |  2.4 KB

  1. Path: sparky!uunet!mcsun!hp4at.eunet.co.at!sca
  2. From: sca@gese.ge14.mdadv.gv.at (Petzi Schweda)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Parameter passing & data abstraction
  5. Date: 31 Dec 92 07:49:04 GMT
  6. Organization: MD-ADV, Vienna, Austria
  7. Lines: 36
  8. Distribution: world
  9. Message-ID: <1992Dec31.084904@gese.ge14.mdadv.gv.at>
  10. References: <1992Dec30.184135.1963@organpipe.uug.arizona.edu>
  11. NNTP-Posting-Host: gese.ge14.mdadv.gv.at
  12. Originator: sca@gese0n.ge14.mdadv.gv.at
  13.  
  14.  
  15. In article <1992Dec30.184135.1963@organpipe.uug.arizona.edu>, dave@cs.arizona.edu (Dave Schaumann) writes:
  16. |> It has come to me that C's mechanisms for parameter passing are less than
  17. |> wonderful.  For instance, in defining some data structure, my typical
  18. |> stratagy is to create a .h file with the struct definition, appropriate
  19. |> typedefs, and #defines for simple functions.
  20. |> 
  21. |> The root of the problem is that I want to pretend that macros are really
  22. |> in-line functions.  However, "macro functions" display "call by reference"
  23. |> behavior, while real functions have call by value behavior.
  24. |> 
  25. |> One solution is to always pass pointers -- in effect, change the default
  26. |> parameter behavior (for structs, anyway) to be call-by-reference.
  27.  
  28. After coding a whole bunch of system modules I would consider this:
  29. No exported macros at all - use functions instead ...
  30. Furthermore highly encapsulate thy data, not exporting any structures:
  31.     #ifdef MODULE_PRIVATE
  32.     typedef struct ModuleDataRec
  33.         {....
  34.         } ModuleDataRec, *ModulData;
  35.         #define SetMember(d, v) (*d).member = v; 
  36.     #else
  37.     typedef void *ModulData;
  38.     void SetMember(ModulData d, int v);
  39.     #endif;
  40. This technique was forced by using runtime libraries, where you can't ensure that anyone using your module will recompile with a new version (btw for me this would be a contradiction to rtl's) ...
  41. For the VERY few uses where the above will lead into performance-troubles, grant them MODULE_PRIVATE, with all it's disadvantages ...
  42. On performance: I think that on risc systems the calling overhead is no problem after all (on DEC Alpha, the first six parameters are passed through registers, and there's nearly no call frame to be built ...), under VAX-VMS (and on other systems doing lots on the stack) it can get a problem ...
  43.  
  44. regards,
  45. ###################################################
  46. Petzi Schweda (sca@gese.ge14.mdadv.gv.at)
  47. MD-ADV, Municipality of the City of Vienna, Austria
  48.  
  49.  
  50.