About link to pointers

I am making a tree because planting trees will save the planet (or just a program).

class Tree {
  Node* root;
  // ...
  void insert(int value){
    private_insert(value, root);
  }
  void insert_private(int value, node* n){
    if(n == nullptr){
      n = new node(value);
    } else {
      your standard recursive insertion function here
    }
  }
  // ...
};

      

Long story short, I tried using shared_ptrs at first, but the insert () function will never add any items to my tree. I thought I might be doing something wrong with shared, so I tried raw pointers and I had the same undetectable reserves.

It turns out I need to pass a link to my root / nodes.

void insert_private(int value, node*& n) {...};

      

I understand that if I do not submit something as a reference, a copy will be made. But if the pointer has an address, doesn't it copy the same address? if i create a new () unspecified pointer why doesn't it bind to my root / nodes?

Why is my question here, I can accept it that it works like this, my tree works, but I don't know why it is.

edit: after reading the comments, I created this little expert level program:

void fn(int* i){
  cout << "Address of local in fn before change: " << i << endl;
  i = new int(2);
// so basically here we made a new int, and i will get the address of 
// this integer and will point to there, what we passed on before 
// becomes irrelevant
  cout << "Address of local in fn after change: " << i << endl;
}
void fn2(int **i){
  cout << "Address of local in fn2 before change: " << i << endl;
  *i = new int(2);
  cout << "Address of local in fn2 after change: " << i << endl;
}
int main(){
  int* p = nullptr;
  cout << "Address of p: " << p << endl;
  fn(p);
  cout << "p& is: " << &p << endl;
  fn2(&p);

  cin.get();
  return 0;
};

      

Thanks everyone for the answers, it helped a lot. random.org will determine who is who gets the approved response thing.

+3


source to share


5 answers


Yes, it is a copy and it contains the same address, but you only assign the copy, which is then discarded when the function returns. The original is not modified. It's your problem.



As an aside, IMHO, if you change the value of a parameter, you must use a pointer, hence a pointer to pointer in your case. This makes it much more obvious to the reader that you are changing the meaning.

+4


source


n = new node(value);

is the destination.



The pointer gets a new value. Now he points to a different place. The pointer was passed by value, so the calling code won't see any changes - the assignment had a local effect.

+2


source


In insert_node

you change what points n

.

If you want this reflected to the caller, you need to pass the pointer n

by reference: void insert_private(int value, node*& n)

although I prefer node** n

it as this signals to the caller that the parameter value may change.

Although, since it root

is a member of the class, you can change it directly in insert_private

.

+2


source


But if the pointer contains an address, does it copy the same address?

Until you change the address stored in the copy, of course.

+2


source


Your design aside "why?": A pointer is an ordinary variable. Changes made to the local copy of the called party are not reflected on the caller's side.

void foo (int a)
{ // here: a is a copy of whatever will be passed to foo
  a = 2;
}
void foo2 (int &a)
{ // here: a is something like 'a local name' for the int passed to foo2
  a = 3;
}

int x = 0;
foo(x); // inside foo(x), a is another int copied from x
// x still 0 here
foo2(x); // inside foo2(x), a is 'a local name' for x
// x == 3 now

      

Pointers behave in the same way as just variables containing an address.

void bar (int * a)
{
  a = new int;
}
void bar2(int * &a)
{
  a = new int;
}

int * x = nullptr;
bar(x); // inside bar(x), a is a new pointer copied from x
// x still nullptr here
// (and memory leaking from the new without delete)
bar2(x); // inside bar2(x), a is 'a name' for the passed pointer x
// x contains memory address from a new int
delete x;
// can be deleted -> no leak

      

+1


source







All Articles