1

I saw in couple of tree codes that a function of tree class has both * and & with node for example to insert a node in a BST a function is like this

insertnode(node * &t,string value)
{
t = new node; t-> val = value 
// code to find right place in BST
}

I wish to know why we pass reference to pointer in general and also in particular to this case . Please also mention if there is any other scenario of this ,Thanks

Instead of posting another question . Can someone also point out the use of object class ? i mean using instances of object class did it allocate all the memory gone for all sub classes ? i.e int float etc. etc.

B Faley
  • 17,120
  • 43
  • 133
  • 223
sparkling_spark
  • 85
  • 2
  • 10

7 Answers7

6

Pointers, as any other variables, are passed by value, unless you specify you want to pass it by reference.

void foo(int* x)
{
   x = new int[1];
}

void goo(int*& x)
{
   x = new int[1];
}

int* x = NULL;
foo(x);
//x is NULL here, and you also have a memory leak
goo(x);
//x points to an array of 1 int

In your case, you pass a reference to a pointer because you want to modify the original pointer. Modifying a pointer doesn't mean changing the value it points to, but changing the address to which it points to.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

If you manipulate a pointer parameter in a function, the pointer value will not be saved after returning from function. But if you pass the pointer by reference, the new address pointer is refering to will be maintained after returning:

void foo1(int* x)
{
    x = new int(2);
}

void foo2(int* &x)
{
    x = new int(3);
}
int main()
{
    int* x = new int(1);
    foo1(x);
    printf("x = %d\n", *x); // x = 1 => x is not referring to a new address after returning from foo1()
    foo2(x);
    printf("x = %d\n", *x); // x = 3 => x is referring to a new address after returning from foo2()
    return 0;
}

Of course there will be memory leak here if you don't delete the previously allocated memory.

B Faley
  • 17,120
  • 43
  • 133
  • 223
1

It's pretty much equivalent to the following, but is somewhat shorter and perhaps easier to parse visually:

insertnode(node** t, string value)
{
  *t = new node;
  (*t)->val = value;
}
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

It means the function can modify the pointer (i.e. change where it points to).

James M
  • 18,506
  • 3
  • 48
  • 56
0

Passing a pointer by reference enables the function to change the pointer directly. In this particular case, it make the pointer point to the newly created node.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

by passing a reference to a pointer, it means you can modify the pointer.

You have an example here: http://msdn.microsoft.com/en-us/library/1sf8shae%28v=vs.80%29.aspx

0

when you declare a function like this :

foo(node* param);

param is actually a pointer passed by value..so you have a copy of that pointer..

while if you pass a parameter:

foo(node*& param);

what you have is a pointer passed by reference so you have an alias of the pointer! alias means same pointer with a different name.

andrea.marangoni
  • 1,499
  • 8
  • 22