473,503 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C function return value?

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
8 13982
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
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 (40°39.22'N, 111°50.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
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
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
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
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
# 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2676
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
12
3255
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
6
2727
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
64
3314
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
4
7674
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
17
3216
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
27
3092
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
8
2809
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
7
10285
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
2
3124
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.