Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cca!mirror!ima!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.c Subject: Re: Buffer referencing between functions. Message-ID: <9576@think.UUCP> Date: Wed, 21-Oct-87 12:57:16 EDT Article-I.D.: think.9576 Posted: Wed Oct 21 12:57:16 1987 Date-Received: Sat, 24-Oct-87 07:37:11 EDT References: <10756@sri-spam.istc.sri.com> Sender: usenet@think.UUCP Reply-To: barmar@sauron.UUCP (Barry Margolin) Organization: Thinking Machines Corporation, Cambridge, MA Lines: 32 Keywords: *(auto_variables) in calling functions? In article <10756@sri-spam.istc.sri.com> robert@sri-spam.istc.sri.com (Robert Allen) writes: [example deleted] > In the example above "called()" depends on the fact that the > pointer passed to the automatic variable 'dbuf' in "calling()" > is correct. > > ... My question is, is it > technically correct to reference an 'external' variable via a > pointer in this manner? Yes, it is fine. A pointer to an automatic variable is valid until the function in which the automatic variable was declared returns. So, it is OK to pass such pointers to called routines. What WOULDN'T be valid would be for calling() to end with return &dbuf; (assuming calling() were declared to return char* rather than void, of course). This would return a pointer to deallocated storage, which would be useless (and potentially hazardous) to the caller. In between is the case of storing such a pointer in another non-automatic structure; you must make sure that this field is never dereferenced between the time calling() returns and the time it is given a new value. --- Barry Margolin Thinking Machines Corp. barmar@think.com seismo!think!barmar