Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > What is a stack...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 195 Topic 24771 of 27348
Post > Topic >>

What is a stack frame?

by jacob navia <jacob@[EMAIL PROTECTED] > Mar 3, 2008 at 11:45 AM

Recently K. Thompson asked me to specify what did I understand
with "stack frame". I think that it is im****tant for people using the
language, that this im****tant concept is clear, so I have produced
this post.

Obviously, there are some people here that live in some
special world where all those things do not exist. They should stay
there and ignore this thread. The same goes for all people that
program in coffee machines and other primitive processors that
do not have a stack pointer or enough registers to use a stack
efficiently.

----------------------
What is a stack frame?
---------------------

Within a stack, each procedure builds a "stack frame", i.e. a fixed
point within the stack, where storage for local variables is reserved.

The stack frames are a linked list of this storage areas. This builds
the conceptual stack that the language needs. Each stack frame is
linked to the previous one. A new stack frame is built when a
procedure is entered, and it is popped from the stack of stack frames
at the procedure exit.

The "main" function has the first (visible) frame, and each stack
frame built in one of the functions called after "main" started is
linked to that stack frame. This makes it possible for debuggers
and other tools to "walk the stack" of called procedures.

We can (conceptually) describe the stack frame as follows

struct tagStackFrame {
	void *Previous;
	void *ThisFrame;
	char LocalStorage[];
};

The "Next" member is a pointer to the previous function's stack frame,
the "ThisFrame" pointer points to the current stack frame, and the
Local storage is made out of different variables that a function
declares. We can imagine that the local storage of deeper nested
blocks is added to the function's local storage, even if it is not
visible (i.e. can't be accessed) after its scope disappears.

Building the stack frame (Prologue)
------------------------

The first thing to do to build a stack frame is then, to save in the
"Previous" slot, the value of the current stack frame pointer.
Normally, this is a dedicated register. For instance, the power PC
version of lcc-win uses register 31. This register is normally
defined by the operating system ABI (Application Binary Interface)
since ALL programs running in the same operating system MUST agree
as to what the frame pointer register is, if not CHAOS is surely the
consequence.

After saving the current FP (or Frame Pointer) we copy the current
value of the stack pointer into the frame pointer, to establish a new
stack frame from this stack position on. We fill then, the "ThisFrame"
slot of our structure, and lastly, we subtract from the frame pointer
the amount of space needed by the function's local variables, our
"Local storage" member.

There exist MANY variation of this scheme. Some optimizing compilers
save themselves the trouble of having a different register than the
stack pointer to build a stack frame, and just use the stack pointer to
address local variables. Conceptually however, nothing changes.

Popping the stack frame (Epilogue)
-----------------------

A function must restore the previous stack frame before leaving the
scene. This is done in the reverse order of the previous actions. The
space allocated for local variables is released by adjusting the stack
pointer, the previous value of the stack frame is popped from the stack,
and the function can now return.

Other stuff
-----------

A function uses registers to do its calculations. Those register can be
scratch registers, i.e. registers that can be used without having to
save them, or they can be registers that need save before use, to
preserve their value across function calls. The saved registers are part 
of the stack frame, even if I did not mention that above to keep things
simple. They are restored before the current function exits.

Another thing to be known is "alloca". This function allocates storage
from the stack by decreasing the stack pointer. A function that calls
alloca must do the popping of the current stack frame in a different
manner since the current stack frame is "deformed" by alloca.

And yet another thing: in standard C, we can have objects whose size
is allocated in local storage but whose exact size is unknown until
run time. This is similar to alloca, and produces the same consequences.




-- 
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 




 195 Posts in Topic:
What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 11:45:35 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-03 10:53:04 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-03 17:57:39 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 13:44:16 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-03 13:02:41 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-03 19:06:34 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 09:07:08 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 09:58:38 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 19:27:43 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-03 12:50:48 
Re: What is a stack frame?
CBFalconer <cbfalconer  2008-03-03 17:55:06 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 23:08:13 
Re: What is a stack frame?
Neil <NeilKurzm@[EMAIL  2008-03-06 00:58:53 
Re: What is a stack frame?
CBFalconer <cbfalconer  2008-03-03 17:49:18 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-04 02:28:51 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-04 02:15:15 
Re: What is a stack frame?
"Bartc" <bc@  2008-03-03 11:33:08 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 13:26:31 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-03 12:38:55 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 13:40:57 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-03 13:14:21 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 14:27:01 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-03 19:11:12 
Re: What is a stack frame?
Randy Howard <randyhow  2008-03-03 13:54:24 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-03 19:31:56 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-03 13:53:03 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 14:55:47 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-03 14:11:19 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 09:11:58 
Re: What is a stack frame?
"J. F. Lemaire"  2008-03-03 21:22:50 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 21:37:17 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-04 07:37:28 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-04 08:38:06 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-04 16:55:25 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-04 10:25:49 
Re: What is a stack frame?
"J. F. Lemaire"  2008-03-04 19:01:25 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-04 23:51:38 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-04 21:23:22 
Re: What is a stack frame?
Antoninus Twink <nospa  2008-03-03 14:15:25 
Re: What is a stack frame?
polas <nick@[EMAIL PRO  2008-03-03 06:26:08 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-03 20:01:48 
Re: What is a stack frame? [OT]
Morris Dovey <mrdovey@  2008-03-03 08:50:22 
Re: What is a stack frame? [OT]
polas <nick@[EMAIL PRO  2008-03-03 08:47:11 
Re: What is a stack frame?
Chris Torek <nospam@[E  2008-03-03 14:55:53 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 09:36:45 
Re: What is a stack frame?
Chris Torek <nospam@[E  2008-03-04 19:39:03 
Re: What is a stack frame?
"Dik T. Winter"  2008-03-05 13:16:21 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-05 08:03:44 
Re: What is a stack frame?
"Dik T. Winter"  2008-03-07 12:59:57 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-07 09:54:21 
Re: What is a stack frame?
Kaz Kylheku <kkylheku@  2008-03-03 09:39:16 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 19:02:26 
Re: What is a stack frame?
Kaz Kylheku <kkylheku@  2008-03-03 11:46:22 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 21:10:18 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-04 03:34:39 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-03 09:40:37 
Re: What is a stack frame?
Michael Mair <Michael.  2008-03-03 22:03:02 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-03 22:17:35 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-04 19:38:19 
Re: What is a stack frame?
Antoninus Twink <nospa  2008-03-06 14:40:04 
Re: What is a stack frame?
"J. J. Farrell"  2008-03-09 22:49:49 
Re: What is a stack frame?
Geoff <geoff@[EMAIL PR  2008-03-09 18:18:56 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-10 14:30:53 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 13:18:55 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 14:06:01 
Re: What is a stack frame?
Charlton Wilbur <cwilb  2008-03-09 21:38:42 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-10 10:41:49 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 10:37:12 
Re: What is a stack frame?
"Dik T. Winter"  2008-03-12 11:26:25 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 13:32:14 
Re: What is a stack frame?
"Dik T. Winter"  2008-03-12 13:28:47 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-13 01:34:45 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-13 12:39:35 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-13 01:38:40 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-13 01:43:00 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-13 10:55:25 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-13 13:15:25 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-13 18:39:45 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 11:51:27 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 12:23:52 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-12 06:01:47 
Re: What is a stack frame?
Charlton Wilbur <cwilb  2008-03-12 10:23:08 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-12 06:39:37 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-12 14:13:14 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-12 14:31:12 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-12 16:33:16 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-12 17:05:25 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 18:24:46 
Re: What is a stack frame?
Nick Keighley <nick_ke  2008-03-13 01:45:24 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-10 23:23:17 
Re: What is a stack frame?
richard@[EMAIL PROTECTED]  2008-03-10 22:52:25 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 00:38:32 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-11 13:47:11 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 07:10:03 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-11 20:19:43 
Re: What is a stack frame?
Ben Bacarisse <ben.use  2008-03-12 21:57:35 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 01:53:18 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-11 14:13:01 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 02:47:52 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-11 15:37:28 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 03:54:51 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 08:24:33 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 09:16:52 
Re: What is a stack frame?
Antoninus Twink <nospa  2008-03-11 10:21:29 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-11 10:01:31 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 15:49:46 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 15:04:58 
Topicality was: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-11 11:11:10 
Re: Topicality was: What is a stack frame?
Antoninus Twink <nospa  2008-03-11 18:35:26 
Re: Topicality was: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-11 18:16:03 
Re: Topicality was: What is a stack frame?
Chris Dollin <chris.do  2008-03-12 09:10:47 
Re: Topicality was: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-12 09:16:52 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-11 15:27:27 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-11 16:31:29 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 12:34:39 
Re: What is a stack frame?
"Dik T. Winter"  2008-03-12 12:00:13 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-13 07:17:45 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-12 21:22:52 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-13 11:22:42 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-12 23:11:46 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-11 18:15:06 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 16:11:21 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 17:42:14 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-11 18:06:17 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 16:43:39 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-12 08:57:02 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 18:27:01 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 16:45:25 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:32:16 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 12:12:47 
Re: What is a stack frame?
Antoninus Twink <nospa  2008-03-11 20:21:10 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:19:43 
Re: What is a stack frame?
David Resnick <lndresn  2008-03-11 19:36:55 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-11 11:40:15 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:15:45 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:17:12 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-11 18:21:19 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 18:41:01 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-12 07:50:32 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-11 20:23:07 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:18:52 
Re: Topicality was: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:25:51 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 19:49:19 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-11 22:00:57 
Re: What is a stack frame?
Charlton Wilbur <cwilb  2008-03-12 01:30:54 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:03:31 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:04:31 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-11 13:30:11 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:31:10 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-11 20:47:56 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-12 09:09:22 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:34:04 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:34:59 
Re: What is a stack frame?
Keith Thompson <kst-u@  2008-03-11 23:48:39 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-11 20:51:36 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-11 19:25:00 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 12:32:19 
Re: What is a stack frame?
gazelle@[EMAIL PROTECTED]  2008-03-12 14:17:49 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 12:33:07 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-12 13:36:55 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 18:20:32 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-12 23:14:03 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 18:54:55 
Re: What is a stack frame?
lawrence.jones@[EMAIL PRO  2008-03-12 14:30:53 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-12 22:25:09 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-12 17:40:30 
Re: What is a stack frame?
Ben Pfaff <blp@[EMAIL   2008-03-12 10:43:38 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-12 18:50:45 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 20:28:43 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-12 21:11:38 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-13 07:12:26 
Re: What is a stack frame?
Lew Pitcher <lpitcher@  2008-03-12 14:34:50 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 20:33:12 
Re: What is a stack frame?
Lew Pitcher <lpitcher@  2008-03-12 16:13:32 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 22:26:44 
Re: What is a stack frame?
Morris Dovey <mrdovey@  2008-03-12 17:50:59 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-13 11:20:24 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-13 15:42:39 
Re: What is a stack frame?
Flash Gordon <spam@[EM  2008-03-13 18:47:58 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-14 09:25:30 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-13 09:49:19 
Re: What is a stack frame?
rlb@[EMAIL PROTECTED] (R  2008-03-13 09:00:03 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-13 09:33:13 
Re: What is a stack frame?
lawrence.jones@[EMAIL PRO  2008-03-13 11:15:48 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-13 16:29:05 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 17:09:45 
Re: What is a stack frame?
Chris Dollin <chris.do  2008-03-13 09:50:25 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 17:22:11 
Re: What is a stack frame?
Richard <devr_@[EMAIL   2008-03-12 17:23:59 
Re: What is a stack frame?
Richard Heathfield <rj  2008-03-12 17:02:53 
Re: What is a stack frame?
Ben Pfaff <blp@[EMAIL   2008-03-12 10:18:15 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 18:58:13 
Re: What is a stack frame?
santosh <santosh.k83@[  2008-03-12 23:34:17 
Re: What is a stack frame?
Ian Collins <ian-news@  2008-03-13 07:06:51 
Re: What is a stack frame?
jacob navia <jacob@[EM  2008-03-12 20:36:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 8:18:53 CDT 2008.