Interpreting the internal mechanism of pointer and reference through disassembly code

Source: Internet
Author: User

It is difficult to use natural languages such as Chinese and English to describe the similarities and differences between C ++ pointers and references in a concise and transparent manner, reading the disassembly code that contains pointers and references is a good way: this is definitely a non-ambiguity and concise statement as much as possible.
For example, the following code
Void main (void)
{
Int x1 = 0, * ptrx1 = & x1;
Int x2 = 0, & refx2 = x2;

(* Ptrx1) ++; // focus on variable operations
Refx2 ++;

Printf ("x1 = % d, x2 = % d/n", x1, x2 );
}
After the execution, the indirect operations on the variables through pointers and references increase x1 and x2 by 1. But how is this implemented? Let's analyze the disassembly Code together:
(* Ptrx1) ++;
Mov edx, dword ptr [ebp-8]; first read the pointer to edx x1
Mov eax, dword ptr [edx]; then read the value of x1 to eax
Add eax, 1; Increase the value of x1 by 1 in eax
Mov ecx, dword ptr [ebp-8]; read x1 pointer again to ecx
Mov dword ptr [ecx], eax; returns the variable value added by 1
Refx2 ++;
Mov edx, dword ptr [ebp-10h]
Mov eax, dword ptr [edx]
Add eax, 1
Mov ecx, dword ptr [ebp-10h]
Mov dword ptr [ecx], eax
Obviously, adding 1 to the referenced variable by reference is exactly the same as adding 1 to the variable pointed to by pointer, this indicates that the reference form adopts the same method as the pointer in the simple form of operation variables: location through the address and operation variables.
In addition to address location and operation variables, the reference does not provide address operations and lacks the flexibility of pointer access variables. The advantage of referencing is concise and suitable for parameter passing of functions, such:
Void chg (int * x, int & y) // The first parameter is a pointer and the last parameter is a reference.
{
X ++;
Y ++;
}

Int main (void)
{
Int mx = 0, my = 0;
Chg (& MX, my );
}

Analysis disassembly code:
Chg (& MX, my); // both pointer and reference PASS Parameters through the address
Lea eax, [ebp-8]
Push eax
Lea ECx, [ebp-4]
Push ECx
Call @ ILT + 0 (chg) (00401005)
X ++; // X ++ modify the address
MoV eax, dword ptr [EBP + 8]
Add eax, 4
MoV dword ptr [EBP + 8], eax
Y ++; // y ++ modify the variable through the address
MoV ECx, dword ptr [EBP + 0ch]
MoV edX, dword ptr [ECx]
Add edX, 1
MoV eax, dword ptr [EBP + 0ch]
MoV dword ptr [eax], EDX
Apparently:
① Pointers and references are located through addresses and operation variables, which are in common;
② Reference operating variables in the form of Operating Common variables: simple; not having the function of operating variable addresses: less flexible and powerful than pointers -- this is the difference between pointers and references.
Through the above analysis, I don't know if we can think like this: References Replace the function of pointers under certain conditions in a simple way; References cover some functions of pointers; pointers can replace references, the reference cannot replace the pointer.
The above "analysis" process should actually be called "interpretation", because the disassembly Code has provided an absolutely unambiguous, concise, and humanized description of the pointer and reference functions, there should be no obstacle for anyone who knows assembly language-this is what I should say by the way: assembly language is the omnipotent key to studying advanced languages.

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.