c++ – Doubt about address in pointer memory

Question:

I put a cout inside the insertRight method to check the memory addresses of node1 (from the variable itself and not from the one it keeps) and to my surprise the addresses shown were repeated several times, as shown below.

004FF97C
004FF97C
004FF894
004FF97C
004FF894
004FF7AC
004FF97C
004FF894
004FF7AC
004FF6C4

Note that one of the local pointers has the same address on two consecutive calls to the insertRight function

As they are local pointers and therefore local variables (I believe, am I right?), shouldn't they be destroyed at the end of the method execution and be allocated to another memory address on the stack in a future call?

Code:

NodeTree.h

class NodeTree
{
public:
    int value;
    NodeTree *right;
    NodeTree *left;

    NodeTree() {
        value = 0;
    }
};

BinaryTree.h

#include <iostream>
using std::cout;
using std::endl;
#include "NodeTree.h"


class BinaryTree
{
public:
    NodeTree * root;
    BinaryTree();
    void insert(int);
    void insertRight(NodeTree *, NodeTree **);
};

BinaryTree.cpp

#include "BinaryTree.h"

BinaryTree::BinaryTree() {
    root = NULL;
}

void BinaryTree::insert(int num) {

    NodeTree *node = new NodeTree;
    node->value = num;
    insertRight(node, &root);
}

void BinaryTree::insertRight(NodeTree *node1, NodeTree **root1)
{
    cout << &node1 << endl;

    if (*root1 == NULL) {
        node1->left = NULL;
        node1->right = NULL;
        *root1 = node1;
    }
    else {
        insertRight(node1, &(*root1)->right);
    }
}

Test_Pointers.cpp

#include "BinaryTree.h"

int main()
{   
    BinaryTree tree;
    tree.insert(5);
    tree.insert(10);
    tree.insert(15);
    tree.insert(20);
    tree.printInOrder(tree.root);
    return 0;
}

Answer:

Starting with the first statement you made:

and to my surprise the addresses shown were repeated several times

This is expected for the code you have, the insertRight , because whenever you do an insert, it will start at the root and navigate to the right until you get a null. Soon it will start browsing the root 4 times, causing the root node to be displayed 4 times. If you had placed a visual separator between each entry as a \n would have been easier to interpret the result.

See the impression you have, identifying each node and each insertion:

004FF97C //insert 1 raiz
004FF97C //insert 2 raiz
004FF894 //insert 2 raiz->direita
004FF97C //insert 3 raiz
004FF894 //insert 3 raiz->direita
004FF7AC //insert 3 raiz->direita->direita
004FF97C //insert 4 raiz
004FF894 //insert 4 raiz->direita
004FF7AC //insert 4 raiz->direita->direita
004FF6C4 //insert 4 raiz->direita->direita->direita

Now let's move on to the question you asked later:

As they are local pointers and therefore local variables (I believe, am I right?), shouldn't they be destroyed at the end of the method execution and have other addresses in a future call?

The pointers are local but the object created is not, and you can confirm this by creating it with the new operator that allocates memory on the heap and not on the stack :

void BinaryTree::insert(int num) {

    NodeTree *node = new NodeTree;

That's why the objects are allocated in memory and keep the same address until they are destroyed with the delete operator. The NodeTree *node local pointer you created is destroyed but the object it points to is not, so you can access it with any other pointer that points to the same place.

You even keep this pointer in a field of your class when you do *root1 = node1; inside the insertRight function.

Also note that if it was not allocated with new the object would be destroyed at the end of the function, but that already made its code incorrect and presenting undefined behavior, and that, with bad luck, it could even work. So if you are going to use objects that you created in a function elsewhere, you need them to be allocated in the heap through new .

Scroll to Top