0

Below I have attached my code. In the below code I am building a binary tree with 3 nodes. My program is printing different values at same address when passed in different function. And there are 4 functions in total

  1. main
  2. build
  3. inorder
  4. print

in the inorder function, I am doing the inorder traversal of the tree and saving that in a vector<Node*> so later I can print the inorder traversal in the "print" function. I am printing the address and the value at each address in all the 4 functions. Getting different values in some functions at the same address. Why is that so?

#include <bits/stdc++.h>
using namespace std;
struct Node
{
    int data;
    Node *left;
    Node *right;
    Node(int val)
    {
        data = val;
        left = right = NULL;
    }
};

Node *build()
{
    Node *root = new Node(50);
    root->left = new Node(45);
    root->right = new Node(60);

    cout << "In Build Function\n";

    cout << root << " " << root->data << endl;
    cout << root->left << " " << root->left->data << endl;
    cout << root->right << " " << root->right->data << endl;

    return root;
}

void inorder(Node *root, vector<Node *> &A)
{
    if (root == NULL)
        return;

    inorder((root)->left, A);
    free(root->left);

    A.push_back(root);

    cout << root << " " << root->data << endl;

    inorder(root->right, A);
    free(root->right);
}

void print(vector<Node *> &A)
{
    cout << "\nprint in function\n";
    for (int i = 0; i < A.size(); i++)
        cout << A[i] << "   " << A[i]->data << endl;
    cout << endl;
}
int main()
{
    Node *root = build();

    cout << "\nIn Main Function\n";

    cout << root << " " << root->data << endl;
    cout << root->left << " " << root->left->data << endl;
    cout << root->right << " " << root->right->data << endl;

    vector<Node *> A;
    cout << "\nIn inorder Function\n";
    inorder(root, A);

    print(A);
    cout << "\nIn Main Function\n";

    cout << root << " " << root->data << endl;
    cout << root->left << " " << root->left->data << endl;
    cout << root->right << " " << root->right->data << endl;
}

output :

In Build Function
0x1d6bc0 50
0x1d6bd8 45
0x1d6bf0 60

In Main Function
0x1d6bc0 50
0x1d6bd8 45
0x1d6bf0 60

In inorder Function
0x1d6bd8 45
0x1d6bc0 50
0x1d6bf0 60

print in function
0x1d6bd8   1928224
0x1d6bc0   50
0x1d6bf0   60


In Main Function
0x1d6bc0 50
0x1d6bd8 1928224
0x1d6bf0 60
  • 2
    don't `free` stuff you `new`ed. `free` pairs with `malloc` and friends. `new` pairs with `delete`. – user4581301 Sep 18 '20 at 05:04
  • Besides the `new` and `free` mismatch, you also attempt to free pointers that you still need. When you push to the vector, you push the *pointers*. If you free those pointers then the pointers in the vector becomes invalid. – Some programmer dude Sep 18 '20 at 05:10
  • 1
    Om an unrelated note, please read [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Sep 18 '20 at 05:12

3 Answers3

1

Problem 1

Don't use free to deallocate memory that was allocated with new. Use delete.

Problem 2

You are deallocating memory and then using that memory to access the values. This causes undefined behavior. Don't deallocate until you are done usign the objects.

Use of #include <bits/stdc++.h>

Don't do it. See Why should I not #include <bits/stdc++.h>?.


Commenting out the calls to free removed the problems for me. See https://ideone.com/9gs8NL. However, that leaves the code in an unclean state. You should add code to deallocate the Node objects before returning from main.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
0

this happening because in inorder function you are using free keyword in root->left and root->right due to which the memory is de-allocated but the pointer still points to same location and prints the garbage value there.There for in later functions it prints the garbage value. BY the way there is no need to use free here.

0

You are using free where you shouldn't. You allocated the nodes with new and you should use delete to free it (as user4581301 already mentioned). The reason you are getting unrelated numbers is you are freeing the data that you are pointing to before you are done with it. If you remove the free lines (which should be delete) then you will see the expected behavior.

I would recommend using a debugger to see when the data you are pointing to has changed.

Sei4or
  • 36
  • 3