Hi guys I'm pretty confused with this,so I'm passing in the address of numbers into the returnPointer function,this is what I thought was happening I thought the address of numbers that I passed in was modified and now pointed to the address of the int y in the function returnPointer but when I return to the main function and run the program the address is still the same as it was before I passed number into the function,how come it only changed the value of number but not the actual address of number?
thanks in advanced,
Adam
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int *returnPointer(int* x){
int y = 55;
x = &y;
return x;
}
int main()
{
int number = 60;
cout << &number << number << endl;
int *num = returnPointer(&number);
cout << &number << *num << endl;
}
Hi,
Your returnPointer() returns the memory address of a local variable (y). When a function exits it destroys all its local variables, that's why when you dereference that memory address, you just see garbage because the value has been already destroyed since the function exited.
Yes. You return the address of y, which is a local variable within function returnPointer. That is something which should not be done, because y no longer exists after the function ends.
how come the address can not be changed?
The compiler allocates the variable number at a particular address (on the stack). You can't move it, it is the compiler's job to take care of the variables which it allocates.
just when I think I am getting the hang of pointers ((I've studied chapters and the material on here so many times ) I hit a road block
for example here on line 9 ,I thought I would have modified the value of number inside the function so I expected for the number to now be 55 yet when I run the program after teh function the number still stays at 60 yet I was expecting it to be 55 so confused =(
Several things are going on:
Let's walk through your program.
Line 14: The compiler allocates space for number on the stack. Let's call this location stack[0].
Line 15: You print the address of stack[0] and then the contents of stack[0].
Line 16: The compiler allocates space for a pointer (num). Let's call this stack[1]. You call returnPointer passing a copy of the address of stack[0]. This copy is at stack[2].
Line 7: The compiler allocates space for y. This is at stack[3].
Line 8: You modify the argument (passed pointer x) which is at stack[2] with the address of y, which is stack[3]. You're NOT changing what x points to nor are you changing the pointer in the caller.
Line 9: You return x, which is &stack[3]. Top of stack is restored to stack[2]; Therefore, stack[3] is now out of scope and is invalid.
We return to line 16, which stores the returned value (&stack[3]) into num (stack[1]).
Line 17: You print the address of number (which hasn't moved). It's still at stack[0]. You then print what num points to, which is stack[3]. It's a memory location, but it's not valid since top of stack is now stack[2].
If you have trouble following this, work it out with pencil and paper.
just a question relating to the breakdown that i don't quite get on lines 16 and 8
Line 16: The compiler allocates space for a pointer (num). Let's call this stack[1]. You call returnPointer passing a copy of the address of stack[0]. This copy is at stack[2].
Line 8: You modify the argument (passed pointer x) which is at stack[2] with the address of y, which is stack[3]. You're NOT changing what x points to nor are you changing the pointer in the caller.
on line 16 I thought I was by passing by reference not by value(copy)?
and on line 8,but I thought x is a pointer so when I modify x I thought I was modifying where it is/was pointing to?
on line 16 I thought I was by passing by reference not by value(copy)?
I assume you mean line 17. That not a reference. The & in this context is the addressof operator.
and on line 8,but I thought x is a pointer
Yes, it is.
o when I modify x I thought I was modifying where it is/was pointing to
Nope. You're modifying the local copy of the pointer.
If you wanted to modify what x points to, then you would have to use *x. But that's going to create a type mismatch because you would be trying to assign a pointer (&y) to an int (number).
I assume you mean line 17. That not a reference. The & in this context is the addressof operator.
I always thought when the function takes a pointer as one of its parameters you pass it by reference so that the original copy would/could be changed by the function?
so sending pointers into functions as parameters is different than sending(treated differently) variables into functions?
No. You have to get your head around the fact that pointers are data types, just like int or doubles, etc. You can pass pointers just like you can pass simple data types. A pointer has the added benefit that you can dereference it (access what it points to).
could you give me an example on how I would send a pointer as an arguement to a function
You did that in your second example above.
int function(int * x)
Think of it this way:
1 2 3 4
typedefint * pointer_to_int; // Create a name for a data type
int function (pointer_to_int x) // Pass pointer by value just like any other data type
int function (pointer_to_int & x); // Pass pointer by reference