Re: How function works internally



"kondal" <kondal04@xxxxxxxxx> writes:
Keith Thompson wrote:
marydeepthy@xxxxxxxxx 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.)

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 <kst-u@xxxxxxx>
| wrote:
|
| >Barry Schwarz <schwarzb@xxxxxxxxx> writes:
| >[...]
| >> 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
| >> implementation 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_Keith) kst-u@xxxxxxx <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.
.



Relevant Pages

  • Re: Local variables controversial?
    ... >>>Calforth uses local variables, but doesn't set a policy of what's good ... they are implemented using a stack frame. ... is still on the Forth data stack? ...
    (comp.lang.forth)
  • Re: Local variables controversial?
    ... Calforth uses local variables, but doesn't set a policy of what's good ... of simply popping the old stack pointer off the stack frame and ... Do I understand you right to mean that the actual data in the stack frame is still on the Forth data stack? ...
    (comp.lang.forth)
  • Re: Buffer overflow prevention
    ... > I have an idea on buffer overflow prevention. ... > the fact that we're using the stack, referenced by SS:ESP pair, both ... > procedure return address and for local variables. ... if we use first segment for passing variables ...
    (Bugtraq)
  • Re: Local variables controversial?
    ... >> you keep them on the stack the stack is minimally 4 deep, ... >Graphics always comes up when locals are discussed. ... I can summarize my ... >that don't ever require substandial stack thrashing OR local variables. ...
    (comp.lang.forth)
  • Re: Patching
    ... >That's what we call an execution vector. ... >is relatively common practice. ... >Common terminology in Forth is to call stack items "stack ... "Local variables" are items delivered to a word on the ...
    (comp.lang.forth)