What is the stack?

The stack is where all the functions’ local (auto) variables are created. The stack also contains some information used to call and return from functions. A “stack trace” is a list of which functions have been called, based on this information. When you start using a debugger, one of the first things you should learn is how to get a stack trace. The stack is very inflexible about allocating memory; everything must be deallocated in exactly the reverse order it was allocated in. For implementing function calls, that is all that’s needed. Allocating memory off the stack is extremely efficient. One of the reasons C compilers generate such good code is their heavy use of a simple stack. There used to be a C function that any programmer could use for allocating memory off the stack. The memory was automatically deallocated when the calling function returned. This was a dangerous function to call; it’s not available anymore.

Showing Answers 1 - 3 of 3 Answers

samiksc

  • Jan 19th, 2006
 

Stack is a last in first out data structure.

Compare it with a box full of books -- you can take out only the uppermost book and can put a new book only at top.

Push() is an operation which puts an element onto the stack. Pop() is an operation which returns and deletes the topmost element on the stack.

OS uses stack for function calls -- if F1() calls F2() then on the stack F1() will be below F2(). So F1() can continue execution only when F2() completes and returns control back to F1().

  Was this answer useful?  Yes

sandeep yadav

  • Mar 11th, 2006
 

The stack is where all the functions? local (auto) variables are created. The stack also contains some information used to call and return from functions.
 
stack has LIFO (last in first out structure)
 
Example --> fucation a() calls another function say b() then at the top of the stack the function b() is there and it returns the value to calling function i.e
a() which is just below the b() on the stack.
 
 
generaly stack has its two function
push () --> to insert an elemant at the top in the stack
pop () --> to delete an elemant from the top of the stack

  Was this answer useful?  Yes

Syed

  • Jun 21st, 2007
 

Please tell me, how I can extract stack information(runtime) of the code being executed using C. The program is running on Windows machine.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions