View C ++ (const attribute) 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]

 

 

 

 

Const is a keyword in C/C ++, but if it is used well, it can greatly improve the robustness of the Code. In general, const is used in many places, but mainly in the following aspects: (1) protection of common variables; (2) Protection of address space; (3) declaration and Protection of class initial variables; (4) Protection of class variables in functions. The protection of const mainly comes from the compiler layer and has nothing to do with program running.

 

(1) Protection of common variables

 

 

Const int data = 10;

Const char str = 'a ';

Const double pi = 3.14;

Const int data = 10;

Const char str = 'a ';

Const double pi = 3.14;

The above Code defines a set of global variables. If the variables in the function are modified, the code compilation will fail.

 

 

 

(2) address space protection

 

 

Void process ()

{

Int value = 10;

Const int * address = & value;

}

Void process ()

{

Int value = 10;

Const int * address = & value;

}

What is different from the above Code is that if the value pointed to by the address changes, the code compilation will fail? You can add * address = 100 before the function ends?

 

 

 

(3) definition of Class const member variables

 

 

Class desk

{

Const int price;

Public:

Desk (): price (10 ){}

~ Desk (){}

};

Class desk

{

Const int price;

Public:

Desk (): price (10 ){}

~ Desk (){}

}; The const member variable is to add the const keyword before the class variable definition. Unlike common member variables, const variables must be initialized in constructors. If there is no const keyword, it doesn't matter whether Initialization is required within the constructor.

 

 

 

(4) const Functions

 

 

Class desk

{

Int price;

Public:

Desk (){}

~ Desk (){}

Void print () {return ;}

Void print () const {return ;}

};

Class desk

{

Int price;

Public:

Desk (){}

~ Desk (){}

Void print () {return ;}

Void print () const {return ;}

}; Is the print and print () const functions in the desk the same? Let's take a look at this Code:

 

53: desk m;

0040122D lea ecx, [ebp-10h]

00401230 call @ ILT + 75 (desk: desk) (00401050)

00401235 mov dword ptr [ebp-4], 0

54: m. print ();

0040123C lea ecx, [ebp-10h]

0040123F call @ ILT + 70 (desk: print) (0040104b)

55: const desk n;

00401244 lea ecx, [ebp-14h]

00401247 call @ ILT + 75 (desk: desk) (00401050)

0040124C mov byte ptr [ebp-4], 1

56: n. print ();

00401250 lea ecx, [ebp-14h]

00401253 call @ ILT + 65 (desk: print) (00401046)

57 :}

53: desk m;

0040122D lea ecx, [ebp-10h]

00401230 call @ ILT + 75 (desk: desk) (00401050)

00401235 mov dword ptr [ebp-4], 0

54: m. print ();

0040123C lea ecx, [ebp-10h]

0040123F call @ ILT + 70 (desk: print) (0040104b)

55: const desk n;

00401244 lea ecx, [ebp-14h]

00401247 call @ ILT + 75 (desk: desk) (00401050)

0040124C mov byte ptr [ebp-4], 1

56: n. print ();

00401250 lea ecx, [ebp-14h]

00401253 call @ ILT + 65 (desk: print) (00401046)

57 :}

The above is a code for function calling. Rows 53 and 55 define the variable m and n of the desk type. Then, in lines 54 and 56, we use two variables to call the print function for processing. We found that the addresses of the two called functions are different, one of which is 0x0040104b, the other one is 0x00401046. We can continue to follow up and take a look:

 

00401046 jmp desk: print (00401310)

0040104B jmp desk: print (004012e0)

00401046 jmp desk: print (00401310)

0040104B jmp desk: print (004012e0)

Although there are two jump functions, the facts have proved that the two functions are indeed different, which proves that our judgment is correct.

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.