confused with this

Pages: 12
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>

using namespace 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.
but how come the address is still the same both times before and after I call the function?
> How come the address is still the same both times before and after I call the function?

Let me rephrase the question :
"How come the address is still the same both times even when the function is running and after the function exits?"

Is that right?
yes how come the address does not change even though I changed it in the function?
Which address are you talking about here? int number at line 14 is not affected by the function call.

I thought the address of numbers that I passed in was modified
No, that isn't what the code does. You pass a pointer to number, which allows the value stored in number to be modified. Its address cannot change.
the address of number

I thought I was returning a pointer(an address)

how come the address can not be changed?
Last edited on
I thought I was returning a pointer(an address)
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.


thanks Chervil for the reply much appreciated

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 =(


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int *returnPointer(int* x){

  int y = 55;
  x = &y;
  *x = y;
  return x;
}

int main()
{
   int number = 60;
   cout << &number << " " << number << endl;
   int *num = returnPointer(&number);
   cout << &number << " " << number << endl;

}
Last edited on
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.




Thanks Anon for the breakdown really helps a lot

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).


Thanks for the reply again anon



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?
It is possible to pass a pointer by reference. However, to do so, the & would be in the function signature of the called function. Not the call.

You can also pass a pointer to a pointer, which also makes it possible to change the pointer in the caller.
ohh ok I think I understand now

so if I did it this way VV I would be changing the value of what is ever passed in

int function(int &x)

and if I did it this way VV I would be sending a copy of the variable in

int function(int *x)

right?

1) Yes

2) No. You're sending a copy of a pointer, not a copy of the variable.

ohh ok so sending pointers into functions as parameters is different than sending(treated differently) variables into functions?

could you give me an example on how I would send a pointer as an arguement to a 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
 
typedef int * 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 

thanks for sticking with me really appreciate it I think I get it now =)

just one final question and i'll study through this thread for quite a while

how come the value of x changes after I have called the function in this example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>

using namespace std;

int functionA(int *x){

  *x = 66;
  return *x;
}

int main()
{

  int x = 55;
  cout << x << endl;
  functionA(&x);
  cout << x << endl;

}



but yet the value of &numbers does not change when I edit it in the first example
Last edited on
I also tried passing a pointer by reference in a program too and it just crashed it on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#include <iostream>

using namespace std;

int *functionA(int* &x){

    *x = 70;
    return x;
}

int main()
{

  int *p;
  functionA(p);

}



Pages: 12