tags:

views:

89

answers:

3

I want to know the exact contents of a program stack.

How is the branching operation done?

What is meant by memory dump while debugging a program using gdb?

Does it give the program stack?

TIA, Praveen

A: 

see the link below which can give you a better idea Variables and Memory

it gives stack trace before the memory corruption happens.that stack trace is the series of function calls that were made along with the arguments passed.

Vijay Sarathi
I thought memory dump is different from core dump. Also what are contents of the program stack?Say i have made a recursive call to function 3 times, and the function has 2 local variables, 2 static variables , 2 dynamically allocated pointers and 2 global variables.
Praveen S
@Praveen Static variables aren't part of a stack frame, dynamically allocated pointers are on the heap, and functions can't have global variables or they wouldn't be global. [Ori's answer](http://stackoverflow.com/questions/3090333/what-are-the-exact-contents-of-a-program-stack-in-c/3090400#3090400) covers what's present in a stack frame; there'd be one for each call
Michael Mrozek
@Michael Mrozek - Except maybe calls that were optimized out, inlined or otherwise munged by the compiler... Compilers aren't required to implement any specific calling convention or ABI.
Ori Pessach
@Michael - Well i missed writing the part, I wanted to know what the contents of the stack are if the executable is having different type of variables and during execution there is a recursive call to a single function. Well for the static and global variables, i included them to know where will these be residing.What is the difference between memory dump and core dump? Its possible that i am using incorrect terms and pardon me in that case.As far as i know core dump gives the call stack when a application crashes. But what is memory dump during debugging of program.
Praveen S
@Praveen: There is no difference between a "memory dump" and a "core dump". They are synonyms, because "core" is an old, old term for memory (when computer memory really was made of tiny ferrite cores woven onto a grid of wires). A memory/core dump contains an image of the entire process's accessible memory (although often excluding shared memory regions and large mapped files).
caf
+2  A: 

The stack is a memory area which contains subroutine arguments, local variables and return addresses from subroutines.

On many architectures, Intel's included, the stack grows from the top down - meaning that the stack pointer is decremented every time data is pushed to the stack.

A typical function call sequence will look like this:

  • Push the arguments
  • Jump to the subroutine (which pushes the return address to the stack)
  • In some languages, (Pascal, for example) the base pointer is pushed to the stack.
  • The called subroutine allocates space on the stack for its local variables.

To produce a stack trace, the runtime environment simply scans the stack to determine where the return addresses point to. I wrote "simply," but it's not that simple if the base pointers weren't saved to the stack, because it might be impossible to determine where any stack frames other than the current one are located.

Ori Pessach
+3  A: 

The C language itself doesn't mandate the use of a stack at all - it defines behaviour rather than implementation.

However, in the common case, the program stack is used to store several things:

  • Objects declared with auto storage duration (ie. ordinary, non-static local variables);
  • Function parameters;
  • The return address (the location in the code where execution should resume after a return; or the end of the current function is reached);
  • Memory allocated with the non-standard alloca() function;
  • Temporary values required by the compiler, such as the saved contents of registers, housekeeping information for alloca(), the size of variable-length arrays and intermediate values used in calculations.

This is not an exhaustive list - other, more exotic things like Trampolines are also sometimes stored on the stack. In general, it is a temporary storage area for working items that will not be needed after the current function returns to its caller.

A "backtrace" in a debugger shows some (but not all) of the contents of the stack.

caf
can you pl. give me pointer to alloca() ?
Kedar