473,651 Members | 3,090 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 13995
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.ED U>
Ravindranath Gummadidala <rg**@cse.buffa lo.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.pow ernet.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********@yah oo.com) (cb********@wor ldnet.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.buffa lo.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_Keit h) 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********@aus tarnet.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_Keit h) 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******@HotPO P.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_Keit h) 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
2700
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
3276
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 MFVec operator+(const MFVec& z1, const MFVec& z2) // Global function { MFVec res = z1; res += z2 return res; // WHY???
6
2736
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 work. Nota() - opens new window. fechaNota() - closes the window opened by Nota() here is the code.
64
3369
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 "refactor-ed" to be
4
7684
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
3240
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
27
3111
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 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
8
2822
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
10314
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 IsLvItemCheckedDelegate(ByVal ClientID As Integer) As Boolean Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean If lvServers.InvokeRequired = True Then lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),...
2
3148
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 i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.