C function return value?

  • Thread starter Ravindranath Gummadidala
  • Start date
R

Ravindranath Gummadidala

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.
 
C

Chris Torek

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".
 
B

Ben Peddell

Ravindranath said:
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.
 
R

Richard Heathfield

Ravindranath said:
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.
 
C

CBFalconer

Ravindranath said:
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.
 
K

Keith Thompson

Ravindranath Gummadidala said:
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.
 
K

Keith Thompson

Ben Peddell said:
Ravindranath said:
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.
 
D

Derk Gwen

# 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.
 
K

Keith Thompson

Derk Gwen said:
# 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top