function call

Can anyone please give me a better understanding of how the variables are pushed and poped from a stack during a function call and how the memory is dynamically created for varibles present in the function???
Let assume that you have function void f( int x ) and you call it in the main passing to it a variable declared in the main. Then the process will look like it is shown below by using the container std::stack instead of the program stack.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stack>
#include <iiostream>
#include <iomanip>


std::stack<int> s; // Imagine that it is the program stack

void f( /* int x */ ) // It is  assumed that the function declared as void f( int x )
{
   std::cout << "Stack is empty is " << std::boolalpha << s.empty() << std::endl;


   int x = s.top();

   x = 2 * x;

   std::cout << "x = " << x << std::endl;

   s.pop();
}


int main();
{
   int x = 10;

   std::cout << "Stack is empty is " << std::boolalpha << s.empty() << std::endl;

   std::cout << "x = " << x << std::endl;

   s.push( x ); // these two statements are equivalent to single statement f( x );

   f();         // these two statements are equivalent to single statement f( x );

   std::cout << "Stack is empty is " << std::boolalpha << s.empty() << std::endl;

   std::cout << "x = " << x << std::endl;
}
Last edited on
closed account (zb0S216C)
If you're not familiar with the concept of a stack, here's a brief overview:

A stack is a abstract data-type which imposes rules on how data is stored inside the structure. The stack follows the LIFO, or last-in-first-out, rule. This rules basically means that what ever gets added to the stack first is removed last. An analogy is a stack of places. In order for you to reach the 3rd plate, for example, you must remove the first two pates.

In computing, a stack is used to store automatic variables (a piece of storage who's destructor is called at the end of the scope in which it was declared) and/or objects. A stack consists of two primary operations: pushing (adding to the stack) and popping (removing from the stack). When an object is pushed onto the stack, that object is placed on top of the object that was pushed previously. When an object is popped from the stack, the object that's on top of the stack is removed; thus revealing the next object on the stack.

To see how a stack works, consider this example:

1
2
3
4
5
int main( )
{
    int A_;
    int B_;
}

Let's walk through this. First, "A_" is declared and then pushed onto the stack. "B_" is then also declared and then pushed onto the stack. When "main( )" ends, "B_" is popped from the stack, followed by "A_". This is the LIFO rule.

If you want to know more about scopes, stack-frames, and general stack information, just ask :)

Wazzak
Last edited on
thnx a lot...i still need more information about how variables get popped during function call....and whether it is the called function or the calling function which pops the return value of the function
This information is all on the web mate - get your google finger out..
yeah i just read....i have a little doubt but...i read that that parameters of the function are pushed from right to left...
so if i have
1
2
int i=1;
printf("%d %d %d",i++,i,++i);

++i should be pushed first followed by i followed by i++
thus i++ will be popped first followed by i and then i++
This should give me the output as
1 2 3

instead of
2 2 2

which i guess is wrong
closed account (zb0S216C)
The stack's behaviour depends on the calling convention used. Which convention do you want to know about?

Wazzak
ok please can anyone please explain me why am i getting two different outputs for the same printf statement??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main( )
{
int *j ;
int * fun( ) ;
j = fun( ) ;
printf ( "\n%d", *j ) ;
printf("\n%d",*j);
getchar();
}
int *fun( )
{
int k = 35 ;
return ( &k ) ;
}

please explain me how and when exactly k is pushed and popped from the stack
i have a feeling that this will clear up my doubt
Last edited on
You are returning a pointer to a local variable - your compiler probably warned you (but you probably ignored it).
the first printf funtion will most likely manage to get the right value of k as a parameter (but even that is not gauranteed) - but once that first printf function is called, it will use the stack for it's own local variables and therefor overwrite
the address where k was.

The second printf function will therefore get a corrupted value for k.
Topic archived. No new replies allowed.