Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bbn!rochester!PT.CS.CMU.EDU!sei!sei.cmu.edu!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.c Subject: Re: MSC-all C's Message-ID: <3245@aw.sei.cmu.edu> Date: Thu, 12-Nov-87 09:58:29 EST Article-I.D.: aw.3245 Posted: Thu Nov 12 09:58:29 1987 Date-Received: Sat, 14-Nov-87 19:07:23 EST References: <3094@rosevax.Rosemount.COM> Sender: netnews@sei.cmu.edu Reply-To: firth@bd.sei.cmu.edu.UUCP (PUT YOUR NAME HERE) Organization: Carnegie-Mellon University, SEI, Pgh, Pa Lines: 54 Keywords: functions, memory models In article <3094@rosevax.Rosemount.COM> richw@rosevax.Rosemount.COM (Rich Wagenknecht) writes: > 1) What happens in C when a function returns a value which the > calling line does not assign to a variable? Where does this > value end up? (example: strcpy(str1,str2); ) Could someone > explain the process that is gone through when a function is > called? Can't answer the other question, but this one maybe I can help. If the function returns a "small" value, such as an int, then its last action before returning is normally to compute the result and leave it in a specific register (eg R0). The caller then expects to find it there. So, for instance x = f() will compile as call f store result from R0 (or wherever) into x If you simply call the function thus f() then the result still comes back in R0 but isn't used. So it is destroyed when the code next decides to use R0. The case of a "large" value is different. The proper way to implement this is for the caller to create a temporary and pass a pointer to it. So, given bigf() returning a large value (of a type T, say) x = bigf() compiles into { T temp bigf (&temp) x = temp } which can sometimes be optimised to just bigf(&x). The function of course copies the return value into the thing pointed at by its (hidden) first parameter. If you now just call bigf(), then the compiler still must generate a temporary and actually emit bigf(&temp), otherwise the poor function will write its result through an undefined pointer. C implementors being what they are, I won't assert that all compilers get this right. Finally, an older way of implementing things like bigf() had the function store its result in a static temporary. This is plain wrong, so I won't discuss it further.