home *** CD-ROM | disk | FTP | other *** search
- 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
- From: martelli@cadlab.sublink.org (Alex Martelli)
- Newsgroups: comp.lang.c
- Subject: Re: Unix C Guru NEEDED!
- Message-ID: <1992Nov03.153347.1082@cadlab.sublink.org>
- Date: 3 Nov 92 15:33:47 GMT
- References: <1992Oct29.235150.14222@den.mmc.com> <1992Oct30.070045.4380@fwi.uva.nl> <1992Oct30.164718.27758@den.mmc.com>
- Organization: CAD.LAB S.p.A., Bologna, Italia
- Lines: 72
-
- richard@crowded-house.den.mmc.com (Richard Armstrong) writes:
-
- :Thanks to the many people who took the time to explain the problem with the
- :function I inherited. This is a fantastic group!
- :
- :The problem with the code, was that the result string was created as an
- :automatic character string in the lowest called function. The pointer to
- :that string was passed up as return values to each function that manipulated
- :it. Since the string was created on the stack, the string would be corrupted
- :as functions were returning, and others were called.
- :
- :The four solutions to the problem were:
- :
- :1) Create the string as a static in the function so the pointer to it
- : and the string would remain "static".
- :
- :2) Create the result string as a global, and don't pass it around.
- :
- :3) malloc the string in the function that creates the string, and pass the
- : pointer to it around. (have to worry about freeing it sometime)
- :
- :4) Create the string in main, and pass the pointer to the functions that
- : manipulate it. (this is how I normally write this type of function).
-
- Analysis and suggestions perfectly correct. I just want to suggest a
- further dirty trick that can alleviate some problems when porting code
- written according to suggestion 1).
-
- The worst problem with 1), as some have suggested, is that often people
- will NOT properly strcy() the returned string before calling the
- function again, eg if you have...:
-
- char *
- thefun(int some_arg)
- {
- static char local[16];
- sprintf(local,"{%d}",some_arg);
- return local;
- }
-
- then some call such as printf("%s %s\n", thefun(anarg), thefun(another))
- will cause trouble.
- The only way to PERMIT such calls (presumably just as long as you get
- around to clean up the code all around...) is the dirty trick I was
- speaking about, something on the lines of:
-
- #define MAX_SIMULTANEOUS_OUTSTANDING_CALLS 16
- char *
- thefun(int some_arg)
- {
- static int current_call;
- static char local[16][MAX_SIMULTANEOUS_OUTSTANDING_CALLS];
-
- if(++current_call>=MAX_SIMULTANEOUS_OUTSTANDING_CALLS)
- current_call=0; /* wraparound */
- sprintf(local[current_call],"{%d}",some_arg);
- return local[current_call];
- }
-
- Yes, it IS still filthy (filthier than the simpler static version above,
- given it violates the principle about the only numbers making sense
- being 0, 1, and infinity...), but sometimes it does come in handy
- temporarily.
- (I'll even admit to having ONE function *built* on this design - a thingy
- called debug_string() which takes in a char*, returning a pointer to
- a string "(NULL)" if its arg is 0, else to the first up-to-40 chars or so
- of it formatted to show control characters in a form such as "^G", etc -
- designed for quick-and-dirty printf()'s [or invocation from inside a
- debugger]...).
- --
- Email: martelli@cadlab.sublink.org Phone: ++39 (51) 6130360
- CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia Fax: ++39 (51) 6130294
-