C++ address passing sense

Today, reviewing the data structure feels a little bit.

Write an insertion function for a binary search tree. The function is a recursive function, without further ado, paste the code first

void AVLTree::insertNode(AVLNode* mroot, int item){//Node insertion
	if (mroot == 0){
		mroot = new AVLNode(item);
	}
	else if (mroot->data > item){
		mroot-> balanceFlag ++;
		insertNode(mroot->left, item);
	}
	else if (mroot->data < item){
		mroot-> balanceFlag--;
		insertNode(mroot->right, item);
	}
	else{
		cout << "Item has already in the Tree" << endl;
	}
}

code in main function

avltree.insertNode (avltree.getRoot (), 1);
avltree.insertNode (avltree.getRoot (), 2);
avltree.insertNode (avltree.getRoot (), 3);
Running these three statements, at the end alvtree.getRoot() is still empty (the root node was initialized earlier as empty).

But I'm passing pointers, won't pointer passing change the value of the actual parameter? Why doesn't it change at the end of the run? I asked the classmates next to me and felt that there was nothing wrong with it.

Searching the Internet for the specific meaning of pass-by-value, pass-by-pointer, and pass-by-reference is indeed a mystery.

Passing by value: It is just the passing of the value, only the use of the parameter in the called function, and does not change the value of the actual parameter.

Pointer passing: The essence is also passing by value, but what he passes is the real address of the actual parameter. Therefore, in most cases, the value of the actual parameter can be changed. But there is another example:

    If the address of the pointer changes in the called function, it does not affect the address of the original actual parameter! So, why is the essence passed by value. If the address changes, the actual parameter remains unchanged! The problem with the above program is here.

if (mroot == 0){
	mroot = new AVLNode(item);
}
Here, mroot is given a new address space and his address has changed. The real root node is still unchanged.

Okay, let's go on and finish talking about passing by reference.

Pass-by-reference: Because there are actual parameters, pass-by-reference can refer to actual parameters. At this time, the address of the actual parameter in the main function is passed in the called function, and the address operation here is the operation on the actual parameter. It is worth noting that passing by reference depends on the existence of actual parameters, and does not exist alone.
Finally, the code problem was solved and it was written according to the book (I didn't write it completely according to the book before, I thought I was correct). First rename the AVLNode*

typedef AVLNode* AVLPoint;
In a recursive function, declare it like this

void AVLTree::insertNode(AVLPoint& mroot, int item){//Node insertion
	if (mroot == 0){
		mroot = new AVLNode(item);
	}
	else if (mroot->data > item){
		mroot-> balanceFlag ++;
		insertNode(mroot->left, item);
	}
	else if (mroot->data < item){
		mroot-> balanceFlag--;
		insertNode(mroot->right, item);
	}
	else{
		cout << "Item has already in the Tree" << endl;
	}
}
Use pass-by-reference to solve problems.

The ambiguity of the concept is the crux of the problem. The pointer passing has been regarded as the supreme existence before, but it is still necessary to recognize the concept of pointer.

An inch is longer and a ruler is shorter. Nothing is omnipotent.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325516349&siteId=291194637