-1

How come n still points to a default node after draw()?

I'm trying to pass in a pointer to a node to draw(), and have it point to a newly-created node within draw().

#include <iostream>

using namespace std;

struct node{
   int key;
   struct node *l, *r;
   int level, place;
};

int keyCounter = 0;

void draw(struct node *t, int low, int high, int storey){
   if(storey == 0) return;
   t = new node();
   t->key = ++keyCounter;
   t->level = storey;
   int mid = (low + high) / 2;
   t->place = mid;
   draw(t->l, low, mid, storey - 1);
   draw(t->r, mid, high, storey - 1);
}

void visit(struct node *t){
   if(t != NULL){
      cout << t->key << ' ' << t->level << ' ' << t->place << '\n';
      visit(t->l);
      visit(t->r);
   }
}

int main(){
   struct node *n = new node();
   draw(n, 0, 64, 6); 
   visit(n);
   return 0;
}
code707
  • 1,663
  • 1
  • 8
  • 20
qgsf
  • 5
  • 1
  • 6
    *How come n still points to a default node after draw()?* Because you pass `n` by value. Any changes made to `t` in `draw()` is only a local change to `t`. It does not affect the value of `n` in `main`. – R Sahu Jul 06 '18 at 05:28
  • You are allocating memory twice for the node, once in `main` and then again in `draw` – CinCout Jul 06 '18 at 05:30
  • You pass n by value which becomes t in draw(). Then you don't use t but change it to point to a new node. It's unclear what you are trying to accomplish. – doug Jul 06 '18 at 05:30
  • [How to pass objects to functions in C++?](https://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c) – R Sahu Jul 06 '18 at 05:31
  • 1
    There is no `delete` in your code. No `new` without `delete`! – Thomas Sablik Jul 06 '18 at 07:07
  • [Initializing a pointer in a separate function](https://stackoverflow.com/questions/2486235/initializing-a-pointer-in-a-separate-function-in-c) – Bo Persson Jul 06 '18 at 08:11

3 Answers3

0

In method draw you set t to point on another node.

t = new node();

Now t point on another memory space. It's not the space where n points. When you change t properties you doesn't change node on which n points. Remove row that I specify above.

Ansver
  • 94
  • 9
0

if you really want to change what n points to, use reference to pointer

void draw(struct node& *t, int low, int high, int storey){

Also, do not forget to free pointer you passed. However, will advise you to seriously consider your design again.

code707
  • 1,663
  • 1
  • 8
  • 20
0

Your code is confused as to who allocates n. You do this twice - once in main() and once in draw(). This is obviously not sensible.

So the obvious question is: why pass n to draw() at all, since it does nothing with it? Instead, try this:

struct node *draw (int low, int high, int storey)
{
    if(storey == 0) return nullptr;
    t = new node();
    ....
    return t;
}

And then:

int main()
{
    struct node *n = draw(0, 64, 6); 
    if (n)
        visit(n);
    return 0;
}

Of course, this is 2018 now and you should not really be using raw pointers at all, but that's another story. And having a function called draw allocating an object is also a bit weird. Perhaps it should be called allocate_and_draw.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48