473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How function works internally

Hi,

I would like to know what happens internally when a c programme is
executed. LIke,
when we call a function, all the previous values will be pushed on to
stack and the local variables of that particular function will be
pushed in to the stack. when the function returns, all the local
variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

i would like to know from where we can get this information. any
sites..

i searched alot, but couldnt find.

Thanks in advance

Deepthy

Sep 27 '06 #1
15 4978

marydeep...@gma il.com wrote:
Hi,

I would like to know what happens internally when a c programme is
executed. LIke,
when we call a function, all the previous values will be pushed on to
stack and the local variables of that particular function will be
pushed in to the stack. when the function returns, all the local
variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

i would like to know from where we can get this information. any
sites..

i searched alot, but couldnt find.

Thanks in advance

Deepthy
If you really want to know this, please see the assembly code generated
by gcc. ;-p

Sep 27 '06 #2
ma*********@gma il.com wrote:
like this, what happens when we call a strcpy function. what happens in
stack??

i would like to know from where we can get this information. any
sites..
The specific details of the function call are defined by the
implementation, not the standard.

It is very easy to inspect the procedure call in some implementations .

For example, if you use GCC, just give it the -S switch and look at the
asm output.

Or if you really want to go in depth, you can study the GCC
documentation (for example), http://gcc.gnu.org/onlinedocs/
or the GCC internals (a separate manual).

If you don't use GCC for some reason, you will have to go elsewhere for
documentation.
Sep 27 '06 #3
ma*********@gma il.com writes:
I would like to know what happens internally when a c programme is
executed. LIke, when we call a function, all the previous values
will be pushed on to stack and the local variables of that
particular function will be pushed in to the stack. when the
function returns, all the local variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??
This is not defined by the language, and different compilers can and
do handle this differently.

Not every implementation has a "stack", in the sense of a contiguous
region of memory that grows in a particular direction. There are
implementations that do the equivalent of a heap allocation to
allocate memory for a function call.

There are de facto standards for some systems. I think what you're
looking for is an ABI (Application Binary Interface, I think).
Searching for that might give you some information -- but keep in mind
that anything you find will be system-specific.

If you want to know what the language actually requires, you can get
the latest draft of the ISO C standard by search for "n1124.pdf" .
(It's definitely not light reading.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 27 '06 #4

Keith Thompson wrote:
ma*********@gma il.com writes:
I would like to know what happens internally when a c programme is
executed. LIke, when we call a function, all the previous values
will be pushed on to stack and the local variables of that
particular function will be pushed in to the stack. when the
function returns, all the local variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

This is not defined by the language, and different compilers can and
do handle this differently.

Not every implementation has a "stack", in the sense of a contiguous
region of memory that grows in a particular direction. There are
implementations that do the equivalent of a heap allocation to
allocate memory for a function call.

There are de facto standards for some systems. I think what you're
looking for is an ABI (Application Binary Interface, I think).
Searching for that might give you some information -- but keep in mind
that anything you find will be system-specific.

If you want to know what the language actually requires, you can get
the latest draft of the ISO C standard by search for "n1124.pdf" .
(It's definitely not light reading.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Could you please tell me which compiler does a heap allocation for a
function call. I haven't heard of it and I feel its interesting to
know. Any document/paper would be sufficient.

-kondal

Sep 28 '06 #5
"kondal" <ko******@gmail .comwrites:
Keith Thompson wrote:
>ma*********@gma il.com writes:
I would like to know what happens internally when a c programme is
executed. LIke, when we call a function, all the previous values
will be pushed on to stack and the local variables of that
particular function will be pushed in to the stack. when the
function returns, all the local variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

This is not defined by the language, and different compilers can and
do handle this differently.

Not every implementation has a "stack", in the sense of a contiguous
region of memory that grows in a particular direction. There are
implementation s that do the equivalent of a heap allocation to
allocate memory for a function call.

There are de facto standards for some systems. I think what you're
looking for is an ABI (Application Binary Interface, I think).
Searching for that might give you some information -- but keep in mind
that anything you find will be system-specific.

If you want to know what the language actually requires, you can get
the latest draft of the ISO C standard by search for "n1124.pdf" .
(It's definitely not light reading.)

Could you please tell me which compiler does a heap allocation for a
function call. I haven't heard of it and I feel its interesting to
know. Any document/paper would be sufficient.
Barry Schwartz mentioned one such system here just recently:

| On Sat, 23 Sep 2006 00:10:26 GMT, Keith Thompson <ks***@mib.or g>
| wrote:
|
| >Barry Schwarz <sc******@doezl .netwrites:
| >[...]
| >pop() was a function you introduced to start the discussion. From the
| >code you've shown, there is no push(). How the compiler chooses to
| >handle intermediate results while evaluating expressions is an
| >implementati on detail that will vary from system to system. My system
| >doesn't have a hardware stack and intermediate results are stored in
| >temporary variables which the compiler keeps track of (and attempts to
| >reuse efficiently).
| >
| >Out of curiosity, what system are you using that doesn't have a
| >hardware stack? It would be good to have a specific example in the
| >recurring argument about whether C defines a "stack".
|
| An IBM Multiprise 2003 S/390 running OS/390 2.10.

Though I realize now that (a) he was talking about intermediate
expression results, not function call activation records, and (b) he
didn't actually mention a heap.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 28 '06 #6
learn(and understand) any x86 based assembly lang.
Cheers
Raxit Sheth

Sep 28 '06 #7
Very basic article, for First Time Reader, (even i feel the best is to
learn x86 prog )

http://en.wikipedia.org/wiki/Call_stack

Cheers
Raxit Sheth

ma*********@gma il.com wrote:
Hi,

I would like to know what happens internally when a c programme is
executed. LIke,
when we call a function, all the previous values will be pushed on to
stack and the local variables of that particular function will be
pushed in to the stack. when the function returns, all the local
variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

i would like to know from where we can get this information. any
sites..

i searched alot, but couldnt find.

Thanks in advance

Deepthy
Sep 28 '06 #8
ma*********@gma il.com wrote:
I would like to know what happens internally when a c programme is
executed. LIke,
when we call a function, all the previous values will be pushed on to
stack and the local variables of that particular function will be
pushed in to the stack. when the function returns, all the local
variables will be poped out..

like this, what happens when we call a strcpy function. what happens in
stack??

i would like to know from where we can get this information. any
sites..
This stuff is often implementation and platform specific. For a very
readable overview of some typical ways this is done, see van der
Linden's "Deep C Secrets".
Sep 28 '06 #9
[The context, which the poster below snipped, was questions about
the internal mechanisms of C runtime systems, with particular
respect to function calls.]

In article <11************ **********@h48g 2000cwc.googleg roups.com>
<ra************ @yahoo.co.inwro te:
>learn(and understand) any x86 based assembly lang.
While there is nothing *wrong* with learning any particular assembly
language, it is a huge mistake to learn *only* x86 assembly and
assume that this is the way "everything " works. In particular,
the x86 is largely based on 1970s-era architectures (specifically
the Intel 4004, 8008, and 8080). x86 implementations carry a huge
burden of "backwards compatibility" with a system optimized for
the conditions that held at the time. It is true that, through
great cleverness, modern CPUs that implement the IA32 architecture
squeeze tremendous performance from this klunky instruction set;
but the instruction set itself remains klunky nonetheless.

Compare, for instance, the call sequence:

/* compute three parameters, then: */
push parm3 /* write 3rd parameter to RAM, adjust stack pointer */
push parm2 /* write 2nd parameter to RAM, adjust stack pointer */
push parm1 /* write 1st parameter to RAM, adjust stack pointer */
call func
addl $12,%esp /* remove 12 bytes of parameter from stack */

with:

/* compute three parameters directly into arg registers */
call func
/* no stack adjustment required */

in which we have removed four out of five instructions from the
caller (by using a more-sensible instruction set). Suppose further
that the target function "func" is a "leaf function" -- one that
makes no calls of its own -- so that the x86 version reads, e.g.:

func:
enter /* instruction that builds stack frame; actually faster
to open-code this as several instructions */
... code to work with the three parameters using the frame ptr ...
leave
ret

while the more-modern architecture just does:

func:
... work directly with the parameters in their registers ...
retl /* "return from leaf" */

which eliminates the frame-building (admittedly, a good compiler
can do this on the x86 in some cases, although register pressure
often forces at least some pushes and pops) and 100% of the CPU<->RAM
traffic. The modern architecture's debug system understands that
"leaf functions" do not build stack frames, and use the "return
from leaf" instruction to branch back to the caller (whose return
address is stored in the "caller's return address" register, rather
than RAM, by the "call" instruction).

For a decent smattering of assembly languages, one might learn (at
least the rudiments of) x86, MIPS or SPARC, PowerPC, and ARM.
Investigating the moribund Itanium instruction set, and perhaps
some vector processor instruction sets (Cray or Convex for instance),
might also be worthwhile. Some Data General Nova or Eclipse
experience could also broaden one's concept of "pointers". None
of these are *required* to understand C code, but they will certainly
give you a better idea of the possible ranges for underlying
architectures, and an appreciation for how well (or in some cases
poorly) C maps onto them.

(I might include IBM AS/400 "MIL" above if I had any idea what was
in it. :-) )
--
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.
Sep 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
5948
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an ASP page which produces a fixed width text file for import into a third party legacy application which I don't have control over. --code sample-- Dim strSomeString
3
14929
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10121
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
37
4988
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
23
6505
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I know if Window.Open has been redefined? BACKGROUND:
39
6521
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
33
47604
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
13
2067
by: Anthony de Almeida Lopes | last post by:
Hello, I am wondering why it is not possible to have a function-like macro like the following: #define __nothread(name) do { \ #ifdef _PTHREAD_H \ #warning "name is not a thread safe function" \ } while (0)
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(){
0
8746
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
8523
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
8626
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
7355
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
6178
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...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
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
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.