3
I put a Cout in the way insertRight
to verify
the memory addresses of node1
(of the variable itself and not
of what she keeps) and to my surprise the addresses shown
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
in two consecutive calls to function insertRight
Because they are local pointers and therefore local variables (I believe, am I right?) , they should not be destroyed at the end of the method execution and be allocated to another memory address in 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);
}
}
Teste_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;
}
I got a little confused. This means that if a local pointer is destroyed after the method is executed, the next local pointer that is created in a later method call will have the same memory address (I don’t mean the memory address that is stored as value, but the pointer’s own address) that the previous pointer ? Because the given answer seemed to refer not to the address of the pointer itself but to the address it stores of another element
– Igor PTZ
@Rogi93 "It means that if a local pointer is destroyed after the execution of the method, the next local pointer that is created in a later method call will have the same memory address" - In the beginning no but we have no way of saying, since we don’t manage address assignment, and it can happen by coincidence, but it’s not supposed to affect our code. The answer actually refers to the address it stores and not the variable itself. It is important to realize that when you do
NodeTree *node = new NodeTree;
is actually creating two things, which are: 1) An object in a place [...]– Isac
[...] in memory defined by heap allocator 2) A pointer that points to that object and therefore receives the object address as its value. At the end of the function only the pointer is discarded, ie the local variable. The memory space that was allocated remains unchanged with the data that has been placed there
– Isac
Why my doubt refers to the address of the pointer itself, which is different from the address it stores as value.
– Igor PTZ
@Rogi93 This is another detail that is confusing. What you are printing is the address of a function parameter -
cout << &node1
, in whichnode1
is the parameter and not a variable you created locally. And this parameter comes from a pointer you previously saved in the tree with*root1 = node1
soon the addresses will not change as they are stored in the tree. You saved each of the pointers in the tree.– Isac
I get it. In my case, I could never get the address of the pointer itself, because the return address will always be from the argument passed to the method, correct ?
– Igor PTZ
@Rogi93 No, you can and is showing the pointer address, which in this case is the argument of the function. The detail by which it does not change is because the pointers are stored. Imagine that you have an object in an address and create a pointer for it. If you now store this pointer in a structure, every time you access the pointer address will give the same value as you store it. It would be different to create another pointer that pointed to the same object, in which case the impression of the address would already give a different value (supposedly, we have no way to guarantee).
– Isac
A doubt about that part:"... The detail by which it does not change is because the pointers are stored" this behavior is also valid for a pointer pointing to a simple integer variable of automatic storage ? Ex: int i = 2 ; int *ptr = &i
– Igor PTZ
@Rogi93 Yes works exactly the same way if the
ptr
is stored in a structure, as in your code. Now there is a significant and serious difference, which is in this example theptr
is pointing to an invalid memory address when the function ends, because theint i
will be released at the end of the function (assuming that it is executed within a function that however ends). In his example the pointer was pointing to something allocated withnew
and so remained valid even after the function ended.– Isac