pointer, reference, what?

Started by
15 comments, last by Feldi 20 years, 5 months ago
another thing with references is that you can''t change them to refer to something else after they have been created, which you may want to do depending on your application.
Advertisement
If I understood correctly, necromancer_df isn''t quite correct. In the case of a pointer being passed to a function, you can not change the address that the pointer points to (i.e. make it point to a different object.) You can only change the attributes of the object being pointed at or, by way of pointer math, change some other location in memory. This is because you only receive a copy of the pointer. If you change the address in the pointer your only changing your copy and not the original. If you want or need to change the pointer, you can pass the pointer by reference or pass the pointer by pointer. In which case you cannot change the pointers pointer to point to a different pointer... (add recursion).

Here''s some code to make my point.

void f(int *p)
{
p = new int;
// does not change the pointer that was passed in
// and will cause a memory leak
}

This is how I understand it to work... if I''m wrong feel free to flame.

-Shags


quote:Original post by Shags
If I understood correctly, necromancer_df isn''t quite correct. In the case of a pointer being passed to a function, you can not change the address that the pointer points to (i.e. make it point to a different object.) You can only change the attributes of the object being pointed at or, by way of pointer math, change some other location in memory. This is because you only receive a copy of the pointer. If you change the address in the pointer your only changing your copy and not the original. If you want or need to change the pointer, you can pass the pointer by reference or pass the pointer by pointer. In which case you cannot change the pointers pointer to point to a different pointer... (add recursion).

Here''s some code to make my point.

void f(int *p)
{
p = new int;
// does not change the pointer that was passed in
// and will cause a memory leak
}

This is how I understand it to work... if I''m wrong feel free to flame.

-Shags



No, you''re confusing references with pointers. A pointer can be reassigned, like setting a NULL pointer to "new int". You cannot do that with a reference. For example:
int i=3;int j=5;int &m = i;m = j; // this sets i = j; it doesn''t reassign m m = &j // syntax error, conversion from int* to int m = (int)&j // this sets i to the address of j


~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
CGameProgrammer, I totally agree with you. You CAN assign a pointer to something else or even NULL, but in the case were a pointer is passed to a function, changing it will NOT effect the pointer outside the function because you only got a copy of it.
quote:Original post by Feldi
these two things it seems to do the same
but why would be the 1st method be preffered over the
2nd one, (if it would)

void Point::Set(const Point &p)
{
x=p.x;
y=p.y;
}

It requires no & at the call site. It doesn''t allow the method to modify p. It can be implemented as copy in, copy out to remove aliasing. It removes the need for checks against null.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by Shags
CGameProgrammer, I totally agree with you. You CAN assign a pointer to something else or even NULL, but in the case were a pointer is passed to a function, changing it will NOT effect the pointer outside the function because you only got a copy of it.

Wellll... you''re not really doing a fair comparison. This is fairer:

void Function ( int &i )
{
i = 3;
}

versus this:

void Function ( int *i )
{
*i = 3;
}

Both those work, it''s just that with the pointer, you have to dereference it. References make things simpler because the rereferencing is implied. With a pointer, you cannot say:

void Function ( int *i )
{
i = 3;
}

This is an error. You can set pointers to 0 but not to any other value, *directly*.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Or try:

void Function ( const int &i )
{
use i
}

versus:

void Function ( const int * const i )
{
use *i
}

In which case neither can be reassigned and neither can have the ints they point to changed. This is probably pretty close to how it works under the hood.

Image loads when I''m online!The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.
My stuff.Shameless promotion: FreePop: The GPL god-sim.

This topic is closed to new replies.

Advertisement