sicko86
sicko86

Reputation: 31

Pointer Value LOSS

I have a problem root value is returning to NULL everytime I go out from the insert function I cant really understand why the pointer doesn't keep it's value.

int main(int argc, char *argv[])
{
    int input;
    node* root = NULL;
    while (input >0 ) {
          cout<< "Enter a Number:";
          cin>> input;
          insert (root,input);
    }
    printall(root);
    system("PAUSE");
    return 0;
}

void insert(node* _node,int val)
{

    //#if 0
    cout << "In insert before" << _node;
    if (_node == NULL) {
            _node = new node;
            _node->val = val;
            _node->left = NULL;
            _node->right = NULL;
            return;
    }
    //#endif
    if(_node->val > val) {
        insert(_node->left,val);
    } else if (_node->val < val) {
        insert(_node->right,val);
    }
    return;        
}

Upvotes: 3

Views: 1244

Answers (5)

6502
6502

Reputation: 114559

Short answer: You are using C++ and you forgot to type a character or your keyboard is defective... correct code is

void insert(node *& _node, int value) // note '&'
{
    ...
}

Long answer:

Normally C and C++ functions parameters are "by value" and this means that the called function will receive a copy of what you pass them...

void foo(int x)
{
    x = 42;
}

void bar()
{
    int k = 12;
    foo(k);
    // Here k is still 12, not 42
}

C++ however has the concept of "reference" that allows you to pass a parameter without making a copy and the function can then for example change caller variables. Change foo(int x) to foo(int& x) and the value of k in bar will become 42 after the call.

If you are working with C++ the & character is telling the compiler that the node parameter should be passed by reference and not by value (i.e. you don't want to make a copy of the pointer, but the function should work directly with your variable.

If you are coming from Visual Basic then just remember that all parameters are byval in C and C++ unless told otherwise (and the same happens in most other modern languages).

Actually in C and in most other modern languages there is no way at all to have parameters passed by reference.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882028

If you want to change the root pointer (or any pointer) within a function, you need to pass a pointer to that pointer (or more likely a reference if you're using C++, and I'm pretty sure you are since you're using new). Something like:

void fn (node **pRoot) {
    *pRoot = new node;
}

Both C and C++ are default pass-by-value so changes won't be echoed up to the higher levels.

Consider the following code:

void changeVals (int x, int *py, int &z) {
    x = 1;
    *py = 2;
    z = 3;
}

int X = 0;
int Y = 0;
int Z = 0;
changeVals (X, &Y, Z);

At this point, both Y and Z will have changed but X will still be zero.

It's no different with pointers. If you want to change the pointer itself in a function, it has to be passed as a pointer to it (and dereferenced within the function) or as a reference type.

Upvotes: 1

Aesthete
Aesthete

Reputation: 18848

In addition to all the answers about pointers to pointers here, I would like to point out that your insert function is allocating memory that is never being deleted.

You need to design systems that handle both allocations and deallocations.

Upvotes: 0

Musa
Musa

Reputation: 97707

You are passing root by value so it cannot be modified by the callee, you have to pass it by reference

void insert(node** _node,int val);
insert (&root,input);

Upvotes: 2

Alex D
Alex D

Reputation: 30455

The pointer isn't "losing" it's value. You need to pass a pointer to pointer to node to insert -- then it can "return" a pointer to node through the parameter.

Upvotes: 2

Related Questions