Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ways in assigning a pointer?

Tags:

c++

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?

like image 627
Xiangyu Zhang Avatar asked Jun 17 '16 02:06

Xiangyu Zhang


People also ask

How are the differences different from the pointer?

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.

How are pointers assigned?

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.

Can pointers of different types be assigned to one another?

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 * .

What is difference in pointer?

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.


2 Answers

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.

like image 101
Lei Mou Avatar answered Sep 17 '22 18:09

Lei Mou


No, the two ways should not be the same:

  • The first assignment p = &q is perfectly valid, because q is the actual object in memory, and p is a pointer to it
  • The second assignment p = 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.

like image 35
Sergey Kalinichenko Avatar answered Sep 19 '22 18:09

Sergey Kalinichenko