# can anyone explain this to me? (stacktest.c)

This is a discussion on can anyone explain this to me? (stacktest.c) within the C Programming forums, part of the General Programming Boards category; I am studying about stack and the teacher gave me this example, Code: //stacktest.c #include <stdio.h> void f(int **p){ int ...

1. ## can anyone explain this to me? (stacktest.c)

I am studying about stack and the teacher gave me this example,

Code:
```//stacktest.c

#include <stdio.h>

void f(int **p){
int x = 1;
printf("&x=%p\n",&x);
*p = &x;
}

void g(void){
int a = 123;
printf("&a=%p\n",&a);
}

int main(void){
int *p;
f(&p);
g();
printf("*p=%d\n",*p);
return 0;
}```
and this program gave this output :

Code:
```&x=0xbffff758
&a=0xbffff758
*p=123```
here, the pointer p gave the value of a (=123) variable instead of x (=1), I didnt get the explanation so clearly.

so, anyone?

2. What do you think the numbers starting with bffff actually mean?

What does the fact that they are both the same mean?

What if you printed p with %p inside main?

--
Mats

3. Originally Posted by meriororen
I am studying about stack and the teacher gave me this example,

Code:
```&x=0xbffff758
&a=0xbffff758
*p=123```
here, the pointer p gave the value of a (=123) variable instead of x (=1), I didnt get the explanation so clearly.

so, anyone?
How are you getting this output? &x and &a should be different. Also *p should be an address which is not 123. I compiled the code and got different values, that's why i'm saying it. But I'm also confused regarding whether *p and &x should be same or not. In my opinion they should be same.

4. What do you think the numbers starting with bffff actually mean?

What does the fact that they are both the same mean?

What if you printed p with %p inside main?
1. It is the address of a and x?
2. pointer p points to the same address? (why would it points to the same address ?)
3. I got different address with the a and x variable.

5. How are you getting this output? &x and &a should be different. Also *p should be an address which is not 123. I compiled the code and got different values, that's why i'm saying it. But I'm also confused regarding whether *p and &x should be same or not. In my opinion they should be same.
That is my question

6. Originally Posted by BEN10
&x and &a should be different.
They can be different, but they can be the same since x and a do not exist in the same scope.

Originally Posted by BEN10
Also *p should be an address which is not 123.
In the main function, p is a pointer to int so *p is an int, not an address.

Originally Posted by BEN10
But I'm also confused regarding whether *p and &x should be same or not. In my opinion they should be same.
What do you mean?

Keep in mind that this exercise is supposed to teach meriororen about the stack, but to be pedantic the code has undefined behaviour since p in main points to an object that no longer exists by the time *p is printed.

7. so, lets say if I change p in function f to p1
Code:
```void f(int **p1){
int x = 1;
printf("&x=%p\n", &x);
*p1 = &x;
}```
then when the program is executed, p1 points to p that points to x, then function f ends which mean the object x is pushed out of stack (?), after that, function g is called, pushing a into the stack (which was the same address used by x before) and since p is still pointing to that address, *p returns 123.

is it correct?

8. Indeed. The stack is "recycled" when a function returns, and the next function will reuse the same space - think of it as a the stack of plates in a restaurant: plates are taken to serve guests, then when they are no longer in use put back on the top of the stack. [Ok, so the plates in a restaurant get cleaned, in C you get served "dirty" plates, which is why you need to initialize any local variables too!].

And as a consequence, you must NEVER return the address of a local variable to a calling function - because it's not going to remain there when the function is done.

--
Mats

9. Ok, thanks, I get it now.

10. Originally Posted by meriororen
so, lets say if I change p in function f to p1
Code:
```void f(int **p1){
int x = 1;
printf("&x=%p\n", &x);
*p1 = &x;
}```
then when the program is executed, p1 points to p that points to x, then function f ends which mean the object x is pushed out of stack (?), after that, function g is called, pushing a into the stack (which was the same address used by x before) and since p is still pointing to that address, *p returns 123.

is it correct?
'p' is a pointer to an int. 'p1' is a pointer to a pointer to an int, which means p1 has the address of p and p has the address of x. Now when we return from function 'f' x gets destroyed. 'a' has nothing to do with p and x. Now the address of 'x' i.e &x and 'p' are the same. The value at p i.e '*p' is the value of x but as x is destroyed it is a garbage value. So *p in main should not print 123(it's just a coincidence in your case I guess), rather any garbage value.

11. Originally Posted by BEN10
So *p in main should not print 123(it's just a coincidence in your case I guess), rather any garbage value.
Well, p points to the address that the variable a used to have in f. Which so happens to be the same as x in function g [in this case, it's obviously not SURE that this will be the case - the compiler may generate some other code with different compiler settings, or using a different compiler may change things].

Stack content is generally not cleared when the function exits [Microsoft Compilers do have an option where stack space that has been freed is actually cleared after returning from the function, which makes it possible to detect errors where a pointer to free-stack space is being used], so until the space is being used for a different purpose, the value remains. Add a line of "printf("Hi\n") before the call to printf with *p, and it's highly likely [but not certain] that the value will change.

--
Mats

12. Originally Posted by BEN10
Now when we return from function 'f' x gets destroyed. 'a' has nothing to do with p and x. Now the address of 'x' i.e &x and 'p' are the same. The value at p i.e '*p' is the value of x but as x is destroyed it is a garbage value.
Not necessarily "destroyed" per se for a simple plain-ole-data variable, the current stack pointer is just adjusted (the variable is effectively popped off the stack), the value that was there is still there until it gets overwritten by something else (the next function's local stack data for example) which is what makes it dangerous to use. If it was a C++ class object then the destructor would be called which may or may not involve mangling the data in the memory location of the object on the stack.

What he said (the guy above me).[/edit]

13. And since the forum this is in is the C, rather than C++, there are no destructors.

--
Mats

14. Originally Posted by matsp
Well, p points to the address that the variable a used to have in f. Which so happens to be the same as x in function g [in this case, it's obviously not SURE that this will be the case - the compiler may generate some other code with different compiler settings, or using a different compiler may change things].

Stack content is generally not cleared when the function exits [Microsoft Compilers do have an option where stack space that has been freed is actually cleared after returning from the function, which makes it possible to detect errors where a pointer to free-stack space is being used], so until the space is being used for a different purpose, the value remains. Add a line of "printf("Hi\n") before the call to printf with *p, and it's highly likely [but not certain] that the value will change.

--
Mats
So, do you mean *p actually should be 123.

15. Originally Posted by BEN10
So, do you mean *p actually should be 123.
No, but that it is not unreasonable to believe meriororen's observation that the output when *p was printed in the main function is 123.