Huawei C/C ++ pen questions (3)

Source: Internet
Author: User

1.Incorrect search

# Define max_srm 256

DSN get_srm_no ()
{
Static int srm_no;
Int I;
For (I = 0; I <max_srm; I ++, srm_no ++)
{
Srm_no % = max_srm;
If (my_srm.state = idle)
{
Break;
}
}
If (I> = max_srm)
Return (null_srm );
Else
Return srm_no;
}

 

A:
(1). srm_no no initial value is assigned.
(2 ). the static declaration makes this function a non-reentrant (unpredictable result) function, because the srm_no variable is placed in the global storage area of the program, you can keep the original values for each call. The static declaration should be removed here.

 

2.Write the program running result

Int sum (int)
{
Auto int C = 0;
Static int B = 3;
C + = 1;
B + = 2;
Return (A + B + C );
}
  
Void main ()
{
Int I;
Int A = 2;
For (I = 0; I <5; I ++)
{
Printf ("% d,", sum ());
}
}

Answer: 8, 10, 12, 14, 16

This question is relatively simple. As long as you note that B is declared as a static global variable, the value of B can keep the original value assigned for the next call.

 

3.

Int func (int)
{
Int B;
Switch ()
{
Case 1: B = 30;
Case 2: B = 20;
Case 3: B = 16;
Default: B = 0;
}
Return B;
}

 

Then func (1) =?

A: func (1) = 0. Because there is no break statement, the switch always calculates B = 0.

 

4.

Int A [3];
A [0] = 0; A [1] = 1; A [2] = 2;
Int * P, * q;
P =;
Q = & A [2];

 

Then a [q-p] =?

A: A [q-p] = A [2] = 2; this question tells us the features of pointer operation.

 

5.If int ** A [3] [4] is defined, the memory occupied by the variable is :_____

A: The pointer array is defined here. For 32-bit systems, the pointer occupies 4 bytes of memory space, so the total space is 3 × 4 × 4 = 48.

 

6.Why is the destructor in the cobject Class A virtual function?

During the se interview, many companies like to ask questions about virtual functions. In the MFC class library, the importance of the cobject class is self-evident. In the definition of cobject, we can see an interesting phenomenon, that is, the destructor of cobject is virtual.

In afx. H, cobject is defined as follows:

Class cobject
{
Public:
// Object Model (types, destruction, allocation)
Virtual cruntimeclass * getruntimeclass () const;
Virtual ~ Cobject (); // virtual Destructors are necessary


};

 

Why does the author of MFC think that virtual Destructors are necessary (Virtual destructor are necessary )?
On the May 1997 page of the famous VC tutorial "proficient in Visual C ++ for Windows 95/NT" (Electronic Industrial edition, 99th edition, Hu Yi, Qiu zongming, etc.), there is a saying:
"If the cobject destructor is not virtual, the derived class will not automatically obtain the virtual destructor, when the object is undo, it will cause a problem-only the destructor of the current class can be called, but the destructor of the base class cannot be called..."
I think this explanation is a serious mistake that should not be made in this very good book. It means:
If:

Class cbase
{
Public:
~ Cbase (){};

};

Class cChild: Public cbase
{
Public:
~ CChild (){};

};

Main ()
{
Child C;

Return 0;
}

 

When the previous code is running, when the Automatic Object C in the stack box is revoked, only ~ CChild () without calling ~ Cbase ().
I think anyone familiar with the c ++ Inheritance theory will immediately point out that this is wrong.
When cChild Object C is generated, you must first call the cbase constructor of the base class before calling the constructor of the cChild class. Therefore, when C is revoked, after the cChild class destructor is called, The cbase class destructor will be called (the sequence of calling the Destructor is the opposite to that of the constructor ). That is to say, no matter whether the Destructor is a virtual function or not, when the derived class object is revoked, it will certainly increase the destructor of its base class in sequence.

So why does the cobject class need a virtual destructor?

Take the above Code as an example. If main () has the following code:
...
Cbase * pbase;
CChild C;
Pbase = & C;
...

So when the pbase pointer is revoked, is the cbase destructor or cChild called? It is obviously cbase (static Association ). However, if you change the cbase class destructor to virtual, when the pbase pointer is revoked, the cChild class constructor will be called first, and then the cbase class constructor will be called.

In this example, all objects exist in the stack box, and the object will be automatically revoked when it leaves its scope. It seems that there is no major problem. However, if the constructor of the cChild class allocates memory in the heap and Its destructor are not of the virtual type, the cChild: ~ will not be called when pbase is revoked ::~ CChild (), so as not to release the memory occupied by cChild: cChild (), resulting in Memory leakage.

When the cobject destructor is set to virtual, The destructor of all the derived classes of the cobject class will automatically become virtual, which ensures that under any circumstances, there will be no memory leakage because the Destructor is not called. This is what MFC will cobject ::~ The real reason why cobject () is set to virtual.

Note: The Destructor can be virtual, but not constructor.

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.