Skip to main content
GameDev.net gamedev.net
🔒 Locked

interesting pointer logic

Started by directrix Dec 22, 2002 at 1:15 AM 7 replies 200+ views
Original Post
directrix
directrix
Just when I thought I knew everything there was to know about pointers I discover something new. It''s easier to explain if I show some source code first.
  
void modify(int *x)
{
	int *t = new int;
	*t = 99;
	delete x;
	x = t;
}

int main()
{
	int *num1 = new int;
	*num1 = 256;
	modify(num1);
	cout << *num1 << endl;
	delete num1;
}
  
After calling modify, I expected num1 to point to the address of t. However, this is not the case, and num1 points to a garbage value after the function call. I''ve discover 2 ways around this, one way is to do the following: void modify(int *&x); the second is to have the modify function return the address of t, and set num1 equal to it. what I''m interested in is why the code above does not work? digital radiation
Melraidin
Melraidin
In modify you set the parameter equal to the address of t. That''s fine, except you didn''t pass the pointer as a reference, and so the pointer isn''t modified outside of the function (you''re modifying a copy of the pointer).
foofightr
foofightr
It doesn''t work because x is just a function argument that gets popped off the stack (i.e dissapears) when the function returns.

num1 points to garbage because you deleted its memory indirectly ( delete x; ) and you''re probably in debug build where it purposely puts garbage after you delete soemthing.
Russell
Russell
You are passing the pointer to the modify() function BY VALUE (it''s only a copy of the address). You set x = t, and then at the end of the function, the copy (which is x) no longer exists. You are confusing passing a pointer and modifying what that pointer points to with passing a pointer and modifying that pointer. When you pass a pointer to something, and you modify what it points to, you are not changing the pointer, you are changing some memory location elsewhere. Changing the pointer inside modify doesn''t change it outside of that function. What you are doing is basically the same as doing this:


  
void modify(int x) {
int t = rand();
x = t;
}

void main () {
int num = 5;
modify(num);
cout << num << endl;
}


And after you call modify() here, num is equal to 5, right? Because you only passed a copy to modify(). Give it time to sink in. I remember having this confusion in the past too.

Russell
Zipster
Zipster
Talk about getting some help
directrix
directrix
I think I get it now, for some reason I''ve been under the impression that passing pointers are always pass by reference and there''s no such thing as pass by value with pointers.

Anyway, thanks alot guys, you''ve been a big help.

digital radiation
Alpha_ProgDes
Alpha_ProgDes
quote:
Original post by Russell
You are passing the pointer to the modify() function BY VALUE (it''s only a copy of the address). You set x = t, and then at the end of the function, the copy (which is x) no longer exists. You are confusing passing a pointer and modifying what that pointer points to with passing a pointer and modifying that pointer. When you pass a pointer to something, and you modify what it points to, you are not changing the pointer, you are changing some memory location elsewhere. Changing the pointer inside modify doesn''t change it outside of that function.

Russell

anyone gotta diagram or something to show this....
because this confuses me also...

Beginner in Game Development?  Read here. And read here.  
foofightr
foofightr
It probably helps to think of a pointer as a normal variable, but instead of directly containing data, instead it points to somewhere else that contains data. So you can pass pointers by value, by reference, or you can even pass its address (pointer to a pointer).

And sorry, I don''t even want to try to explain Russell''s post. He juiced it up so much that it almost confused me too! But he''s got a goo point: give it time
Fruny
Fruny
You are confusing passing a pointer and modifying what that pointer points to with passing a pointer and modifying that pointer.

quote:

anyone gotta diagram or something to show this....
because this confuses me also...




typedef int TYPE;

TYPE val;
TYPE* ptr = &val

modify( ptr );

// Modifying what the pointers point to :
void modify( TYPE* x ) { *x = 0; } // val = 0

// Modifying the pointer :
void modify( TYPE* & x ) { x = 0; } // ptr = 0

// Modifying a local copy of the pointer - what the OP did
void modify( TYPE* x ) { x = 0; } // no side-effect


The confusing part is that, in C, you pass a pointer to a value when you want to modify that value. Which means that if you want to modify the pointer you need to pass a pointer to the pointer... That is, in the code above, TYPE would be an int* instead of an int. To further confuse the issue, the duality array/pointer (array[idx] ≡ *(array+idx)), make it so that passing an array to a function lets you modify the values stored in the array - though you cannot affect the array/pointer itself.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.