View the class structure and structure of C ++ from the perspective of Assembly)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

In the previous sections, we discussed C language content, but we have not really touched on C ++ knowledge. After this blog, We will touch more class content. Class has many attributes. Today we will discuss constructor and destructor.

 

 

(1) What if there is no constructor or destructor?

 

 

Copy to clipboardprint? Class apple

{

Public:

Void print () const {return ;}

};

Class apple

{

Public:

Void print () const {return ;}

};

 

Although this class has no significance, if sizeof is used to calculate the size, we find that it still occupies a byte. What if there is an apple variable? To make apple variables meaningful, we try to make a change:

 

 

Copy to clipboardprint? 66: apple;

67: a. print ();

00401248 lea ecx, [ebp-4]

0040124B call @ ILT + 0 (apple: print) (00401005)

68: return;

69 :}

66: apple;

67: a. print ();

00401248 lea ecx, [ebp-4]

0040124B call @ ILT + 0 (apple: print) (00401005)

68: return;

69 :}

We can see that the stack allocates four bytes to a, which is a byte under ebp.

 

 

 

 

(2) When will the Destructor be called?

 

 

Copy to clipboardprint? Class apple

{

Public:

Apple () {printf ("apple ()! \ N ");}

~ Apple () {printf ("~ Apple ()! \ N ");}

Void print () const {return ;}

};

Class apple

{

Public:

Apple () {printf ("apple ()! \ N ");}

~ Apple () {printf ("~ Apple ()! \ N ");}

Void print () const {return ;}

}. If yes, we can create a test environment as follows:

 

 

Copy to clipboardprint? 68: apple;

0040126D lea ecx, [ebp-10h]

00401270 call @ ILT + 65 (apple: apple) (00401046)

00401275 mov dword ptr [ebp-4], 0

69 :{

70: apple B;

00401_c lea ecx, [B]

00400000f call @ ILT + 65 (apple: apple) (00401046)

71 :}

00401284 lea ecx, [B]

00401287 call @ ILT + 0 (apple ::~ Apple) (00401005)

0040128C mov dword ptr [ebp-4], 0 FFFFFFFFh

72 :}

00401293 lea ecx, [ebp-10h]

00401296 call @ ILT + 0 (apple ::~ Apple) (00401005)

0040129B mov ecx, dword ptr [ebp-0Ch]

0040129E mov dword ptr fs: [0], ecx

68: apple;

0040126D lea ecx, [ebp-10h]

00401270 call @ ILT + 65 (apple: apple) (00401046)

00401275 mov dword ptr [ebp-4], 0

69 :{

70: apple B;

00401_c lea ecx, [B]

00400000f call @ ILT + 65 (apple: apple) (00401046)

71 :}

00401284 lea ecx, [B]

00401287 call @ ILT + 0 (apple ::~ Apple) (00401005)

0040128C mov dword ptr [ebp-4], 0 FFFFFFFFh

72 :}

00401293 lea ecx, [ebp-10h]

00401296 call @ ILT + 0 (apple ::~ Apple) (00401005)

0040129B mov ecx, dword ptr [ebp-0Ch]

0040129E mov dword ptr fs: [0], ecx

As we can see, as long as the scope is out, the Destructor will be automatically called.

 

 

 

 

(3) if it is a new call class, will the Destructor be automatically called?

 

No.

 

 

 

 

(4) What is the nature of constructor and destructor?

 

We know that the temporary variables in the function must be initialized when applied in the stack, and will be automatically reclaimed when the stack returns. So the constructor and the Destructor? In fact, this is the same. When a function is called, The stack will also prepare a stack with an appropriate size for such a class, and then call the constructor to initialize such a piece of memory, when returning a function, call another function to clear the resources that may be involved. Resources are not memory space, but system resources in a broad sense, such as IO, socket, lock, paint brush, and dialog box handle. Therefore, if you do not allocate resources in a timely manner in the destructor, it will cause resource leakage. All this will only return to the system when the program ends. However, if a program occupies too many resources, it will inevitably affect other programs.

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.