Re: using local buffers. (fwd)

From: Billy H. Chan (~{3B:FH;~}) (bhchan@csua.berkeley.edu)
Date: 09/18/95


   From: Ron Poulton <ron@res.com>
   Subject: Re: using local buffers.
   
   Btw, I know I can 'return "string"' in order to return a string from a 
   char* function, but if I have a local buffer, how would I return a string 
   in the same manner without returning the address of the local buffer?
   
>From past experience in lower division CS classes, returning a string is
hairy and can cause a slew of seg faults if not done correctly. The solution
was always to do some malloc'ing and free'ing.
Correct me if I'm off, but an un'malloc'ed string (or pointer in general)
returns only the first 'address' of the first character in the string,
(the rest is sequential).  malloc'ed, the data remains tied to the address
returned.  Un-malloc'ed, you don't know when the data in that address may
be written over.
That, I think, was the reason behind the global buf[MAX_STRING_LENGTH]...
mallocing a string here and a string there without freeing them could lead
to memory 'leakage'.
My two gold coins.
-Billy H. Chan	~{3B:FH;~}		    <bhchan@csua.berkeley.edu> 
 For more, check out http://www.csua.berkeley.edu/~bhchan
 NovaCmdr. 'Suicide Strom' of Clan Wolf | FiestyFly FireMoth Variant
 "If you're going under 100kph, you might as well stand still"



This archive was generated by hypermail 2b30 : 12/07/00 PST