Path: utzoo!utgpu!watmath!iuvax!uxc.cso.uiuc.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: best way to return (char *) Message-ID: <18234@mimsy.UUCP> Date: 23 Jun 89 04:57:06 GMT References: <7800013@gistdev> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 29 In article <7800013@gistdev> joe@gistdev.UUCP writes: >... Suppose I am writing a function that is going to construct a >character string, and is going to return a pointer to that string. >What is the best way to do this so that your pointer is sure >to be valid when used? What you are asking is not `How does one go about returning an object of type pointer-to-char?', but rather `Where should one allocate space for the characters?'. This question does not have a single best answer; there is not sufficient information here to choose one. Most of the approaches you listed are reasonable in some contexts, although if I am right in my interpretation of this one: > . Don't worry about it at all -- nothing is going to trash your memory > at the pointed-to address before you can actually use it. it is a bad idea. (My interpretation is that you mean something like char *fn() { char buf[SIZE]; /* but this is automatic storage */ ... return (buf); } This approach is particularly dangerous precisely because it often works.) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris