Thank you for checking my question. I just encountered a really fundamental problem when I am implementing a BST, that is "what the difference in different approaches to assign a pointer?" We all know assign a point can use:
int *p, q;
p = &q;
Or:
int *p, *q;
p = q;
They should be the same. But in my case below, they working totally different:
template <typename T>
void Tree<T>::_insert(TreeNode<T>*& tree, const T& value, const unsigned& key)
{
// TreeNode<T> node(value, key);
// if(tree == nullptr) tree = &node;
TreeNode<T> *node = new TreeNode<T>(value, key);
if(tree == nullptr) tree = node;
else if(key < tree->index) _insert(tree->left, value, key);
else if(key > tree->index) _insert(tree->right, value, key);
else if(key == tree->index) std::cerr << "_insert: repeating key" << std::endl;
}
Using the first way (noted one), the function won't assign tree equal to node, while the second way works fine.
So, is this my fault write, or they naturally different?
References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.
When pointer assignment occurs, any previous association between the pointer object and a target is terminated. Pointers can also be assigned for a pointer structure component by execution of a derived-type intrinsic assignment statement or a defined assignment statement.
You can, but the behavior will be implementation defined as the types are considered to be incompatible. And theoretically the pointers might have different sizes and point to different memories. The only pointer type which is compatible with any other is void * .
Pointers: A pointer is a variable that holds the memory address of another variable. A pointer needs to be dereferenced with the * operator to access the memory location it points to. References: A reference variable is an alias, that is, another name for an already existing variable.
Please notice in the first case:
// TreeNode<T> node(value, key);
// if(tree == nullptr) tree = &node;
node
is an object allocated on stack.
While in the second case
TreeNode<T> *node = new TreeNode<T>(value, key);
if(tree == nullptr) tree = node;
node
is allocated on heap.
The difference is that once _insert
function returns, its stack frame is poped, and all local variables/objects becomes invalid, as a result, you'll have memory errors.
No, the two ways should not be the same:
p = &q
is perfectly valid, because q
is the actual object in memory, and p
is a pointer to itp = q
assigns an unitialized pointer q
to p
, which is undefined behavior.That is why the two implementations are different.
If you would like to assign q
to p
, the q
itself needs to be assigned first. For example, you could assign new int
to it:
int *p, *q = new int;
p = q;
However, in this case you might as well assign new int
directly to p
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With