home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16008 next >
Encoding:
Internet Message Format  |  1992-11-04  |  3.4 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!elroy.jpl.nasa.gov!swrinde!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!rutgers!modus!gear!cadlab!martelli
  2. From: martelli@cadlab.sublink.org (Alex Martelli)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Unix C Guru NEEDED!
  5. Message-ID: <1992Nov03.153347.1082@cadlab.sublink.org>
  6. Date: 3 Nov 92 15:33:47 GMT
  7. References: <1992Oct29.235150.14222@den.mmc.com> <1992Oct30.070045.4380@fwi.uva.nl> <1992Oct30.164718.27758@den.mmc.com>
  8. Organization: CAD.LAB S.p.A., Bologna, Italia
  9. Lines: 72
  10.  
  11. richard@crowded-house.den.mmc.com (Richard Armstrong) writes:
  12.  
  13. :Thanks to the many people who took the time to explain the problem with the
  14. :function I inherited. This is a fantastic group! 
  15. :
  16. :The problem with the code, was that the result string was created as an
  17. :automatic character string in the lowest called function. The pointer to
  18. :that string was passed up as return values to each function that manipulated
  19. :it. Since the string was created on the stack, the string would be corrupted
  20. :as functions were returning, and others were called. 
  21. :
  22. :The four solutions to the problem were: 
  23. :
  24. :1) Create the string as a static in the function so the pointer to it
  25. :   and the string would remain "static".
  26. :
  27. :2) Create the result string as a global, and don't pass it around.
  28. :
  29. :3) malloc the string in the function that creates the string, and pass the
  30. :   pointer to it around. (have to worry about freeing it sometime)
  31. :
  32. :4) Create the string in main, and pass the pointer to the functions that
  33. :   manipulate it. (this is how I normally write this type of function).
  34.  
  35. Analysis and suggestions perfectly correct.  I just want to suggest a 
  36. further dirty trick that can alleviate some problems when porting code
  37. written according to suggestion 1).
  38.  
  39. The worst problem with 1), as some have suggested, is that often people
  40. will NOT properly strcy() the returned string before calling the
  41. function again, eg if you have...:
  42.  
  43. char *
  44. thefun(int some_arg)
  45. {
  46.     static char local[16];
  47.     sprintf(local,"{%d}",some_arg);
  48.     return local;
  49. }
  50.  
  51. then some call such as printf("%s %s\n", thefun(anarg), thefun(another))
  52. will cause trouble.
  53. The only way to PERMIT such calls (presumably just as long as you get
  54. around to clean up the code all around...) is the dirty trick I was
  55. speaking about, something on the lines of:
  56.  
  57. #define MAX_SIMULTANEOUS_OUTSTANDING_CALLS 16
  58. char *
  59. thefun(int some_arg)
  60. {
  61.     static int current_call;
  62.     static char local[16][MAX_SIMULTANEOUS_OUTSTANDING_CALLS];
  63.  
  64.     if(++current_call>=MAX_SIMULTANEOUS_OUTSTANDING_CALLS)
  65.     current_call=0; /* wraparound */
  66.     sprintf(local[current_call],"{%d}",some_arg);
  67.     return local[current_call];
  68. }
  69.  
  70. Yes, it IS still filthy (filthier than the simpler static version above,
  71. given it violates the principle about the only numbers making sense
  72. being 0, 1, and infinity...), but sometimes it does come in handy
  73. temporarily.
  74. (I'll even admit to having ONE function *built* on this design - a thingy
  75. called debug_string() which takes in a char*, returning a pointer to
  76. a string "(NULL)"  if its arg is 0, else to the first up-to-40 chars or so
  77. of it formatted to show control characters in a form such as "^G", etc - 
  78. designed for quick-and-dirty printf()'s [or invocation from inside a
  79. debugger]...).
  80. -- 
  81. Email: martelli@cadlab.sublink.org                   Phone: ++39 (51) 6130360
  82. CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: ++39 (51) 6130294 
  83.