Article View: comp.lang.c.moderated
Article #2041Re: returning multiple values in C
From: Gerald Feldman
Date: Sat, 17 Feb 1996 17:53
Date: Sat, 17 Feb 1996 17:53
45 lines
2575 bytes
2575 bytes
Christian Lassem wrote:
> I am not sure this is related, but in GCC you can implicitly declare the
> _return_ value with a name.
>
> So:
> typedef struct { int n; double y; } ret_type;
>
> ret_type frexp(double x) return r /* Return name. */
> {
>
> r.n = ...;
> r.y = ...;
> }
I think we might be straying from the original intent here, but this is
not a GCC invention, it is a C++ mechanmism. Basically, the actual
calling standards differ widely among implementations. As mentioned
earlier, some implementations use registers to pass values rather than
the stack, and, likewise, use registers to return values. In the case of
multiple values, such as the ldiv_t library function, the function
returns a struct which contains related informaiton. I generally prefer,
in this case, a function returns a status (success or failure), and the
actual results be returned in a struct (or other variables) whose
address is passed as a parameter. The "right" way to do it tends to be a
style issue. Returning a struct generally involves copying the data from
the function's local data (normally on the stack), to wherever the
return value is stored (implementation defined), then the data is copied
to the object of the assignment. For a small struct and a good
optimizing environment, this can be done in registers. For larger
structs, the data is copied twice. Some of the POSIX reentrant functions
pass in an additional pointer to output data. For example, the ctime
function returns a pointer to a string. Since this string is in static
data, the ctime function is inherently not threadsafe. So, the _r
version of the function requres the caller to supply storage for the
string:
char *ctime_r(
const time_t *timer,
char *buffer) ;
--
Jerry Feldman Digital Unix Development Environment
Digital Equipment Corp. Nashua, NH 03062-9987
gaf@zk3.dec.com, gaf@bcs.org
Technical Details
Message-ID:
<4g54nd$ahi@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4fa7gv$2te@solutions.solon.com> <4fbg7p$9u7@solutions.solon.com> <4fu84a$9em@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Gerald Feldman <gaf@zk3.dec.com> Newsgroups: comp.lang.c.moderated Subject: Re: returning multiple values in C Date: 17 Feb 1996 11:53:49 -0600 Organization: Digital Unix Development Environment Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4g54nd$ahi@solutions.solon.com> References: <4fa7gv$2te@solutions.solon.com> <4fbg7p$9u7@solutions.solon.com> <4fu84a$9em@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Mailer: Mozilla 2.0b6a (X11; I; OSF1 T4.0 alpha)