- Interview One: What is the difference between a pointer and a reference?
Answer:
(1) Non-null difference. References to null values cannot be used under any circumstances. So if you use a variable and point it to an object, but that 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 is definitely pointing to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference. The fact that there are no references to null values means that using the referenced code is more efficient than using pointers.
(2) The difference of legality. You do not need to test the legitimacy of a reference before using it. Instead, the pointer should always be tested to prevent it from being empty.
(3) The difference can be modified. Another important difference between pointers and references is that pointers can be re-assigned to point to another different object. But the reference always points to the object that was specified at initialization and cannot be changed later, but the contents of the specified object can be changed.
- Interview question two: Which of the following 5 functions can be successfully exchanged for two numbers?
#include <iostream>using namespacestd;voidSWAP1 (intPintq) { inttemp; Temp=p; P=Q; Q=temp;}voidSWAP2 (int*p,int*q) { int*temp; *temp=*p; *p=*Q; *q=*temp;}voidSWAP3 (int*p,int*3) { int*temp; Temp=p; P=Q; Q=temp;}voidSWAP4 (int*p,int*q) { inttemp; Temp=*p; *p=*Q; *q=temp;} voidSWAP5 (int&p,int&q) { inttemp; Temp=p; P=Q; Q=temp;}intMain () {intA=1, b=2; //Swap1 (A, b); //swap2 (&A,&B); //swap3 (&A,&B); //SWAP4 (&A,&B); //SWAP5 (A, b);cout <<"A:"<< a <<Endl; cout<<"B:"<< b <<Endl; return 0;}
Parsing: This problem examines parameter passing, value passing, pointer passing (address passing), and reference passing.
Swap1 Pass a copy of the value, in the function only modifies the parameter p, Q (actually a, b of a copy), p, Q values are actually exchanged, but they are local variables, does not affect the main functions A and B. When the function swap1 the end of the life cycle, the stack where p and q is located is deleted.
SWAP2 is passed an address, in the function body of the formal parameter *p, *Q is the actual parameter a, B address of two pointers.
Here to note:
int *temp;
*temp=*p;
is illogical, int *temp creates a new pointer (but does not allocate memory). *temp=*p is not pointing but copy. Copy the value of the memory pointed to by the *p (that is, the value of a) to the memory that *temp points to. but is int *temp not allocating memory? Does not allocate, so the system temporarily gives a random address when copying, let it save value. The assigned random address is an "unexpected" and is not recycled after the function has ended, causing a memory leak.
SWAP3 is passed an address, in the function body parameters *p, *q is the actual parameter a, B address of two pointers.
Here to note:
int *temp;
Temp=p;
int *temp a new pointer (but no memory allocated). Temp=p is pointing rather than copying. Temp points to the address that *p points to (that is, a). and the code:
int *temp;
Q=temp;
However, the function SWAP3 cannot achieve two-number exchange because the function body is just a pointer change, and the value in the address does not change.
SWAP4 can implement two-number exchange because it modifies the value in the address pointed to by the pointer.
The SWAP5 function is similar to SWAP4, which is a reference pass, and the result of the modification directly affects the argument.
Answer:
Swap3,swap5
- Interview question three: what is the result of this program test?
#include <iostream>using namespacestd;voidGetMemory (Char*p,intnum) {P=(Char*) malloc (sizeof(Char) *num);}intMain () {Char*str =NULL; GetMemory (str, -); strcpy (str,"Hello"); return 0;}parsing: The problem is that in the function getmemory, the compiler always makes a temporary copy of each parameter of the function, in this case *p in void GetMemory (char *p, int num) is actually a copy of STR in the main function, In the function getmemory, the memory address pointed to by P is changed, but Str is not changed, because the function getmemory does not have a return value, so STR does not point to the memory requested by P, so the function getmemory cannot output anything, as shown in. In fact, every time the getmemory is executed, a piece of memory is applied, but the requested memory is not released effectively, and the result is that the memory is always exclusive, resulting in a memory leak.
C + + notes-pointers and references