Basic knowledge
Talk about the difference between C ++ references & pointers as formal parameters
int n;
int & m = n;
In C ++, there is an extra reference declarator &, which C does not have. As above, m is a reference to n, and m is simply an alias for n. , Any operation on m is the same for n.
For references, there are three rules:
(1) A reference must be initialized when it is created (pointers can be initialized at any time).
(2) There can be no NULL reference, and the reference must be associated with a legal storage unit (the pointer can be NULL).
(3) Once the reference is initialized, the relationship of the reference cannot be changed (the pointer can change the pointed object at any time).
If you dynamically request memory space in a function, using pointers and references as parameters will get different results, such as the following example:
void fun (int * & b) {// Use a reference as a parameter
b = (int *) malloc (sizeof (int) * 3);
for (int i = 0; i <3; i ++) {
b [i] = i;
}
}
If a null pointer of int type is defined in the main function and passed in as an argument, as follows:
int main () {
int * a = NULL;
fun (a);
for (int i = 0; i <3; i ++) {
cout << a [i] << "";
}
cout << "/ n";
return 0;
}
As a result, memory access errors occur with functions that use pointers, and functions that use references run normally and output correctly 1 2 3.
This is because:
1. Although the pointer is an address passed, in fact, a new pointer is also defined in the function to point it to the same address as the passed-in pointer. However, the storage addresses of the two pointers themselves as variables in memory are different, that is to say, these are two different variables, but the content (that is, the address referred to) is the same.
2. Dynamically request memory for the newly defined pointer in the function, but after the function ends, the life cycle of the requested memory is also ended, so when returning to the main function, the pointer address and content as actual parameters have not changed. . It is still a null pointer, and a memory read error naturally occurs when accessing it.
If you write it like this in the main function:
int * a = (int *) malloc (sizeof (int) * 3);
There will be no memory read errors, but the output is still wrong, and the same is true.
3. When a reference is passed as an argument, b in the fun function is actually an alias (or nickname) of a in the main function. Anyway, it is a variable with the same operation, the same address, and the same content. On return, operations on b are also valid for a in the main function.
Take another example:
int * a = NULL;
char * b = (char *) a;
int * a = NULL;
char * & b = (char *) a;
This time it's the difference in the compilation phase:
You can compile with a pointer, but not with a reference, indicating that the type conversion is wrong.
As you can see from these two examples, pointers are more flexible and more dangerous than references.
Excerpt from "High Quality C ++ Programming"
Article 1: The difference between pointers and references
Pointers and references look completely different (pointers use the operators '*' and '->' and references use the operator '.'), But they seem to have the same function. Both pointers and references let you indirectly reference other objects. How do you decide when to use pointers and when to use references?
First, realize that in no case can a reference to a null value be used. A reference must always point to some object. So if you use a variable and let it point to an object, but the variable may not point to any object at some point, you should declare the variable as a pointer, because you can assign a null value to the variable. Conversely, if the variable must point to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference.
PS: References cannot be added with const when they are defined, otherwise compilation errors occur. You can add const before the formal parameters to ensure that the variable will not be modified in the function.
The above talk about the difference between C ++ references and pointers as formal parameters is the entire content that I shared with you, I hope to give you a reference, and I hope everyone supports the script home.