By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

C function return value?

P: n/a
Hi All:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:

"every invocation of a function causes a frame for that function to be
pushed on stack. this contains the arguments this function was called
with, address to return to after return from this function (the location
in the previous stack frame), location of previous frame on stack (base or
start of this frame) and local variables of this function"

my question is how is a value returned from this function. say the value
is stored in some local variable, is the address of this local variable
passed to the caller (this does not seem right because the current frame
gets popped before caller tries to access the location but then again the
value should still be there intact as no changes have been made yet i.e.
another function's frame has not overwritten values there). or is this
return value stored temporarily on the heap and the caller gets it from
there somehow or is the value stored in some register and obtained from
there. in such a case how would a structure be returned? we could store
the struct on heap but say the function does not do that, just creates a
struct object and returns that. how then are values (i
deliberately use values meaning the possible use of a struct to
return multiple values) returned from functions?

Any pointers will be appreciated.

Thanks,
Ravi.
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ravindranath Gummadidala wrote:
Hi All:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:

"every invocation of a function causes a frame for that function to be
pushed on stack. this contains the arguments this function was called
with, address to return to after return from this function (the location
in the previous stack frame), location of previous frame on stack (base or
start of this frame) and local variables of this function"

my question is how is a value returned from this function. say the value
is stored in some local variable, is the address of this local variable
passed to the caller (this does not seem right because the current frame
gets popped before caller tries to access the location but then again the
value should still be there intact as no changes have been made yet i.e.
another function's frame has not overwritten values there). or is this
return value stored temporarily on the heap and the caller gets it from
there somehow or is the value stored in some register and obtained from
there. in such a case how would a structure be returned? we could store
the struct on heap but say the function does not do that, just creates a
struct object and returns that. how then are values (i
deliberately use values meaning the possible use of a struct to
return multiple values) returned from functions?

Any pointers will be appreciated.

Thanks,
Ravi.


There are two possibilities:
1) Registers are used.
On the x86 under GCC, EAX is used for 1, 2, and 4-byte ints.
EDX:EAX is used for 8-byte ints.
(Pointers are 4-byte ints.)
AFAIK, st(0) is used for floats (whether single, double or
long double)
If the -freg-struct-return is used, then structures of 1, 2,
4 or 8 bytes are returned in EDX:EAX.
2) Memory is used.
In all other cases, space is allocated _by the caller_ and a
pointer to that space is passed before the first argument
(i.e. pushed onto the stack last). The function then fills
in this memory with the returned structure.

Nov 14 '05 #2

P: n/a
In article <news:Pi*******************************@hadar.cse. Buffalo.EDU>
Ravindranath Gummadidala <rg**@cse.buffalo.edu> writes:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:

"every invocation of a function causes a frame for that function to be
pushed on stack. this contains the arguments this function was called
with, address to return to after return from this function (the location
in the previous stack frame), location of previous frame on stack (base or
start of this frame) and local variables of this function"
This is *a* method. It is not *the* method. Different C compilers
use different methods. (In particular, one common technique in
use today is not to have the parameters and return-address on any
stack at all, nor to have separate stack and frame pointers. If
the return address is in a register -- as done by the hardware on
MIPS and SPARC and PowerPC, for instance -- then leaf functions,
i.e., functions that do not call other functions, can just leave
it there and "return" using a jump-register instruction. If all
its local variables fit in any registers left over after accounting
for parameter registers, a leaf function often needs no memory at
all!)
my question is how is a value returned from this function.


In a machine- and/or compiler-specific manner.

Three common techniques are to put the return values into registers,
or to return pointers to the return values (again often in registers),
or to have the caller pass, as "hidden" extra arguments, pointers
to the places the return values should go.

If you want to learn the method for a particular machine and/or
compiler, consult the appropriate documentation (if any) or newsgroup
(if any), or read the compiled code or the compiler code. Note
that information gleaned via the latter two methods (reverse
engineering) is often "subject to change without notice".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3

P: n/a
Ravindranath Gummadidala wrote:
Hi All:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:

"every invocation of a function causes a frame for that function to be
pushed on stack.


Stop right there. :-)

How a conforming C implementation implements the function call mechanism is
entirely up to that implementation, and different implementations will
typically do it in different ways.

If you want to know how a specific implementation does this, ask in a
newsgroup devoted to that implementation.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #4

P: n/a
Ravindranath Gummadidala wrote:

I am trying to understand the C function call mechanism. Please
bear with me as I state what I know:

"every invocation of a function causes a frame for that function
to be pushed on stack. this contains the arguments this function
was called with, address to return to after return from this
function (the location in the previous stack frame), location of
previous frame on stack (base or start of this frame) and local
variables of this function"


You don't know this, because it is in error. It may apply to some
implementations, but that is not all. You don't even need to
know, unless you are implementing a C system or other methods of
interacting with a C system.

What you do know is spelled out in the C standard.

Even if you are dealing with a system that uses a stack in the
above general manner, the specifics, order, timing, etc. are all
peculiar to that system, and should be discussed in a newsgroup
specific to that system.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5

P: n/a
Ravindranath Gummadidala <rg**@cse.buffalo.edu> writes:
Hi All:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:

"every invocation of a function causes a frame for that function to be
pushed on stack. this contains the arguments this function was called
with, address to return to after return from this function (the location
in the previous stack frame), location of previous frame on stack (base or
start of this frame) and local variables of this function"
That description is highly implementation-specific. As far as the C
standard is concerned, there may not be such a thing as a stack frame,
or even a stack.
my question is how is a value returned from this function. say the value
is stored in some local variable, is the address of this local variable
passed to the caller (this does not seem right because the current frame
gets popped before caller tries to access the location but then again the
value should still be there intact as no changes have been made yet i.e.
another function's frame has not overwritten values there). or is this
return value stored temporarily on the heap and the caller gets it from
there somehow or is the value stored in some register and obtained from
there. in such a case how would a structure be returned? we could store
the struct on heap but say the function does not do that, just creates a
struct object and returns that. how then are values (i
deliberately use values meaning the possible use of a struct to
return multiple values) returned from functions?


Different implementations return function results in different ways.
One common approach is to return function results (and pass some
parameters) in registers. Another is approach is to return functions
results on the stack; the details (how the stack space is reserved,
whether the caller or the callee restores the stack pointer after the
call, etc.) are implementation-specific.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #6

P: n/a
Ben Peddell <no********@austarnet.com.au> writes:
Ravindranath Gummadidala wrote:
Hi All:
I am trying to understand the C function call mechanism.
[...] There are two possibilities:
1) Registers are used.
On the x86 under GCC, EAX is used for 1, 2, and 4-byte ints.
EDX:EAX is used for 8-byte ints.
(Pointers are 4-byte ints.)
AFAIK, st(0) is used for floats (whether single, double or
long double)
If the -freg-struct-return is used, then structures of 1, 2,
4 or 8 bytes are returned in EDX:EAX.
2) Memory is used.
In all other cases, space is allocated _by the caller_ and a
pointer to that space is passed before the first argument
(i.e. pushed onto the stack last). The function then fills
in this memory with the returned structure.


That's even more implementation-specific that what the OP quoted.
There are many implementations other than x86 and gcc, and many
possible mechanisms other than the ones you describe.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #7

P: n/a
# my question is how is a value returned from this function. say the value
# is stored in some local variable, is the address of this local variable
# passed to the caller (this does not seem right because the current frame

The value is returned in an invisible auto variable defined by the caller
or global variable. The address of the variable is known to the function,
either because it's well-known or it is passed from the caller.

A function that returns an int or double or pointer will nearly always
return the value in a global register variable which is well known due to
the function interface protocol.

A function that returns a value too big or awkward to return in a register
can store it in an auto variable defined by the caller whose address is
passed in or computable from the stack pointers, or for the more inane
implementation, the caller stores the value in a static variable and returns
its address in a register variable.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
GERBILS
GERBILS
GERBILS
Nov 14 '05 #8

P: n/a
Derk Gwen <de******@HotPOP.com> writes:
# my question is how is a value returned from this function. say the value
# is stored in some local variable, is the address of this local variable
# passed to the caller (this does not seem right because the current frame

The value is returned in an invisible auto variable defined by the caller
or global variable. The address of the variable is known to the function,
either because it's well-known or it is passed from the caller.
There may or may not be such an "invisible auto variable", and it may
or may not have an address (in particular, registers typically don't
have addresses).

I suppose you could say that the location used to hold the return
value is "auto" in some sense, in that it has to be allocated in such
a way that recursive calls work properly, but you're discussing
implementation-dependent mechanisms far more than the standard does.
A function that returns an int or double or pointer will nearly always
return the value in a global register variable which is well known due to
the function interface protocol.

A function that returns a value too big or awkward to return in a register
can store it in an auto variable defined by the caller whose address is
passed in or computable from the stack pointers, or for the more inane
implementation, the caller stores the value in a static variable and returns
its address in a register variable.


Specific function return mechanisms are appropriate for discussion in
newsgroups for a particular implementation. As far as standard C is
concerned, function call and return are defined by sections 6.5.2.2,
"Function calls", and 6.8.6.4, "The return statement", of the C99
standard (or similar sections of the C90 standard). These sections
define semantics, not mechanisms.

If you just want to write portable C, you don't need to understand the
specific mechanisms (though knowing about some of them can be
illuminating). If you really need to understand the mechanisms,
you're not writing portable C, and this isn't the newsgroup that can
help you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.