Pointer and reference of [C ++ Interview Questions] (2)

Source: Internet
Author: User
ArticleDirectory
    • 1. What are the results of the following program test?
    • 5. In C ++, there is malloc/free. Why do we still need new/delete?
1. The following Program What are the results of the test?
 
# Include <iostream> using namespace STD; void getmemory (char * P, int num) {P = (char *) malloc (sizeof (char) * num );} int main () {char * STR = NULL; getmemory (STR, 100); strcpy (STR, "hello"); Return 0 ;}

Resolution:In the getmemory function, the compiler always creates a temporary copy for each parameter of the function. In this example, void getmemory (char * P, int num) * P in is actually a copy of STR in the main function. In the getmemory function, only the memory address pointed to by P is changed, but STR is not changed at all because the function getmemory does not return a value, therefore, STR does not point to the memory applied by P, so the getmemory function cannot output anything, as shown in. In fact, each execution of getmemory will apply for a piece of memory, but the applied memory cannot be effectively released. As a result, the memory remains exclusive, resulting in Memory leakage.

 

If you must use a pointer to apply for memory, you should use a pointer pointing to the pointer and pass the STR address to the function getmemory.CodeAs follows:

 
# Include <iostream> using namespace STD; void getmemory (char ** P, int num) {* P = (char *) malloc (sizeof (char) * num );} int main () {char * STR = NULL; getmemory (& STR, 100); strcpy (STR, "hello"); cout <* STR <Endl; cout <STR <Endl; cout <& STR <Endl; return 0 ;}

In this way, the program runs successfully. We can print * STR, STR, and & STR respectively, and we can find that the results are h, hello, and 0024fa80. STR is the string value; * STR is the first character of the string, and & STR is the address value of the string.

Of course, you can also use the function return value to transmit dynamic memory. The Code is as follows:

# Include <iostream> using namespace STD; char * getmemory (char * P, int num) {P = (char *) malloc (sizeof (char) * num ); return P;} int main () {char * STR = NULL; STR = getmemory (STR, 100); strcpy (STR, "hello "); cout <* STR <Endl; cout <& STR <Endl; return 0 ;}

We can extend this question to see how Integer Variables pass values. The Code is as follows:

 
# Include <iostream> using namespace STD; void getmemory1 (int * num) {* num = 5 ;}int main () {int A; getmemory1 (& ); cout <A <Endl; return 0 ;}

Getmemory1 transfers the address of a. * num is the value in the address and a copy of. by directly modifying the value in the address, you do not need to return a value. You have also modified a because the value of the address pointed to by a has changed.

Answer:

The program crashes. Because getmemory does not pass dynamic memory, STR in the main function is always null.

 

2. Write the following program running results.
# Include <iostream> using namespace STD; int main () {int A [3]; A [0] = 0; A [1] = 1; A [2] = 2; int * P, * q; P = A; q = & A [2]; cout <A [q-p] <Endl ;}

Resolution:The program structure is as follows:

(1) declare an integer array a [3] and assign values to the array respectively.

(2) Two integer pointers p and q are declared, but the addresses pointed to by these two pointers are not defined.

(3) point the address of integer pointer P to A (note that A is a [0]), and point the address of integer pointer Q to a [2].

The actual verification procedure is as follows:

 
# Include <iostream> using namespace STD; int main () {int A [3]; A [0] = 0; A [1] = 1; A [2] = 2; int * P, * q; P = A; cout <p <Endl; cout <* P <Endl; Q = & A [2]; cout <q <Endl; cout <* q <Endl; cout <A [q-p] <Endl ;}

The above output results are as follows:

002dfd24

0

002dfd2c

2

2

2

The actual address of Q is 002dfd2c, and the actual address of P is 002dfd24. 002dfd2c-002dfd24 = 0x08 (hexadecimal subtraction). The difference is 8.

The actual Q-P operation is (address value of Q (002dfd2c)-address value of P (002dfd24)/sizeof (INT), and the result is 2.

Answer:

The running result is 2.

 

3. What is the output result of the following code?
# Include <iostream> using namespace STD; Class A {public: A () {M_a = 1; m_ B = 2 ;}~ A () {}; void fun () {printf ("% d", M_a, m_ B) ;}private: int M_a; int m_ B ;}; class B {public: B () {M_c = 3 ;}~ B (); void fun () {printf ("% d", M_c);} PRIVATE: int M_c ;}; void main () {; B * P = (B *) (& A); P-> fun ();}

Resolution:First of all, it is certain that the above code is very poor, whether it is readable or secure. The person who writes this type of code should, according to jarne stroustrup (C ++ flag maker), make a decision ".

But I have to say that this is also a good question for you to understand the memory offset:

B * P = (B *) (& );

This is a brutal conversion. It forces the content of a address to be regarded as a Class B object, and P points to the memory space of Class.

Class B only has one element M_c, but class A's memory space stores the first element at M_a, and P points to the first address of the object's memory, for example, 0x22ff58, but when P-> fun () calls B: Fun () to print M_c, the compiler knows M_c that the offset of M_c from the object is 0, the offset 0x22ff58 + 0 of the first address of object A is printed, that is, the value of M_a.

Answer:

The running result is: 1.

 

4. What is the output result of the following program?
 
# Include <iostream> using namespace STD; Class A {public: int M_a; A () {M_a = 1 ;}void print () {printf ("% d ", m_a) ;}}; Class B: Public A {public: int M_a; B () {M_a = 2 ;}}; int main () {B; B. print (); printf ("% d \ n", B. m_a );}

Resolution:M_a in Class B overwrites M_a in Class. When constructing Class B, first call the constructor of Class A. Therefore, M_a in Class A is 1, B. print () Prints M_a in Class A, and M_a in Class B is 2.

Answer:

12

 

5. In C ++, there is malloc/free. Why do we still need new/delete?

Answer:

(1) malloc and free are standard library functions of C ++/C, and new/delete are operators of C ++. They can be used to apply for dynamic memory and release memory.

(2) For non-Internal data objects, only malloc/free can not meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.

(3) Therefore, the C ++ language requires a new operator capable of dynamic memory allocation and initialization, and a delete operator capable of cleaning and releasing memory. New/delete is not a library function but an operator.

 

Pointers and references are completely written, and there are still a lot of unintelligible ones, such as pointer functions and pointers pointing to pointers. There may be many errors in the above content. please correct me...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.