3 Replies - 764 Views - Last Post: 06 November 2012 - 06:46 PM Rate Topic: -----

#1 ClaudeP   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 27-March 11

Trying to modify a pointer passed as an argument to equal another.

Posted 06 November 2012 - 05:08 PM

Hi guys. I think I have gotten a pretty good idea of how pointers and references work. But I have been suck on this one last thing for the past day. I have a function that looks like this:


void getRight(vector<Obj*> & objectList , Obj * right){
    //Delete the previous object
    delete(right);
    //objectList.at(0)->x equals 40 in this case. 
	right = objectList.at(0);
}


The class object has a member 'x'. someListOfObjects.at(0) is a pointer to an Obj with a value for x of 40.

and if I call this like :

Obj * right = new Obj(20);
getRight(someListOfObjects, right);

cout << right->x << endl;



It will output 20, not 40 like it should. However if I modify the function to be:

void getRight(vector<Obj*> & objectList , Obj * right){
        //Delete the previous object
        delete(right);
	right = new Obj(40);
}


It outputs 40. So it seems I have a problem when I just set right to equal another pointer from my vector. I tried to simplify this as much as I could. Let me know if I didnt include enough information, I cannot figure this out so ANY help is much appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to modify a pointer passed as an argument to equal another.

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Trying to modify a pointer passed as an argument to equal another.

Posted 06 November 2012 - 05:43 PM

When a pointer gets passed to a function, remember that it's simply a copy of the pointer. In other words, your function is only modifying a copy of the original pointer which is being destroyed with the rest of the stack frame when the function returns. To demonstrate explicitly, take this code for example (sorry for using a global, it's just to make things more simple in this case).

#include <iostream>

int val = 44;

void fun(int *ptr) {
	ptr = &val;
}

int main() {
	int *ptr = 0;
	fun(ptr);
	std::cout << *ptr;
	return 0;
}


In main, a copy of 'ptr' is passed to fun and this copy is modified to point to the address of the variable 'val'. Of course, the original pointer in main was never modified, so the program would try to dereference address 0. If you want a function to modify the original pointer, you will need to pass the address of that pointer; a pointer to a pointer. For example.

#include <iostream>

int val = 44;

void fun(int **ptr) {
	*ptr = &val;
}

int main() {
	int *ptr = 0;
	fun(&ptr);
	std::cout << *ptr;
	return 0;
}

This post has been edited by Aphex19: 06 November 2012 - 05:45 PM

Was This Post Helpful? 2
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Trying to modify a pointer passed as an argument to equal another.

Posted 06 November 2012 - 06:15 PM

Since you are using C++, you can also pass the pointer by reference.

void fun(int *&ptr) {
    ptr = &val;
}



Just a rule of thumb, unless you explicitly pass a parameter by reference (using &), you are creating a copy of that parameter.

The code below still passes the double pointer by value, however it's not changing where ptr points to, it's changing where ptr's target (another pointer) points to.
void fun(int **ptr) {
	*ptr = &val;
}


This post has been edited by jjl: 06 November 2012 - 06:18 PM

Was This Post Helpful? 2
  • +
  • -

#4 xZachtmx   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 125
  • Joined: 14-June 09

Re: Trying to modify a pointer passed as an argument to equal another.

Posted 06 November 2012 - 06:46 PM

Wow it makes perfect sense. Thanks very much guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1