References in C ++ from the perspective of Reverse Analysis

Source: Internet
Author: User
Several questions:

1. Does the referenced variable occupy the memory space?

2. How does reference work?

3. How does a pointer work?

4. What is the difference between a reference and a pointer?

1. What is reference

In C ++ primer, the reference is another name of the object, and the reference is another name of the object it binds, all operations acting on the reference actually apply to the object bound to the reference ."


2. Does the reference occupy the memory space?

A piece of C ++ code:

// Reference_Pointer_Local#include <cstdio>int main() {  int  a = 100;  int& ref = a;  int* ptr = &a;  printf("%d %d %d\n", a, ref, *ptr);  return 0;}

Use the/Fas compilation option to compile the Code:

Public_mainextrn_printf: near_datasegment $ sg530db '% d % d', 0ah, region $ =-8_ref $ =-12_ptr $ =-4_mainproc near; 4: int main () {pushebpmovebp, espsubesp, 12; Create stack, reserve 12 bytes; 5: int A = 100; movdword PTR _ A $ [EBP], 100; [ebp-8] is the memory address of; 6: Int & ref = A; leaeax, dword ptr _ A $ [EBP]; obtain the memory address of a: movdword PTR _ ref $ [EBP], eax; [ebp-12] is the ref memory address,; the memory saved is the memory address of a; 7: int * PTR = & A; leaecx, dword ptr _ A $ [EBP]; obtain the memory address of a movdword PTR _ PTR $ [EBP], ECx; [ebp-4] is the memory address of ref; Save the memory address of a; 8: printf ("% d \ n", A, ref, * PTR); movedx, dword ptr _ PTR $ [EBP] moveax, dword ptr [edX]; the pointer PTR indirectly obtains the value pusheaxmovecx, dword ptr _ ref $ [EBP] movedx, dword ptr [ECx], and references ref to indirectly obtain the value pushedxmoveax of, dword ptr _ A $ [EBP] pusheaxpushoffset flat: $ hour, 16; 00000010 h; 9: Return 0; xoreax, eax; 10:} movesp, ebppopebpret0; restore stack _ mainendp_textendsend

The Assembly Code clearly tells us that the reference also occupies the memory space, but the memory address of the bound object is saved in this memory space, which is similar to the pointer!


3. Working Mechanism of reference and pointer

A piece of C ++ code:

// swap_ref_ptr.cpp#include <cstdio>void swap_reference(int& ref_x, int& ref_y) {  int temp = ref_x;  ref_x = ref_y;  ref_y = temp;  return ;}void swap_pointer(int* ptr_x, int* ptr_y) {  int temp = *ptr_x;  *ptr_x = *ptr_y;  *ptr_y = temp;  return ;}int main() {  int x = 4;  int y = 9;  swap_reference(x, y);  swap_pointer(&x, &y);  return 0;}

In this program, both references and pointers share the same thing, that is, they exchange two numbers, the following figure shows the stack representation when the main function calls swap_reference (the same is true when swap_pointer is called ):


Use the/Fas compilation option to obtain the Assembly Code as follows:

Public? Swap_reference @ yaxaah0 @ Z; swap_reference_textsegment_ref_x $ = 8_ref_y $ = 12_temp $ =-4? Swap_reference yaxaah0 @ Z proc near; swap_reference; 4: void swap_reference (Int & ref_x, Int & ref_y) {pushebpmovebp, esppushecx; 5: int temp = ref_x; moveax, dword ptr _ ref_x $ [EBP]; [EBP + 8] stores the X memory address. Do you still remember the ECX movecx pushed when swap_reference is called in the main function, dword ptr [eax]; ECx obtains the value of X movdword PTR _ TEMP $ [EBP], ECx; X is saved in the temp memory space ,; the compiler allocates a stack space for temp; 6: ref_x = ref_y; movedx, dword ptr _ ref_x $ [EBP]; it can be understood that edX points to XM Oveax, dword ptr _ ref_y $ [EBP]; it can be understood that eax points to ymovecx, dword ptr [eax] movdword PTR [edX], ECx; values are indirectly assigned, the result directly affects the value of the original X and Y. 7: ref_y = temp; movedx, dword ptr _ ref_y $ [EBP] moveax, dword ptr _ TEMP $ [EBP] movdword PTR [edX], eax; 8: return; 9:} movesp, ebppopebpret0? Swap_reference @ yaxaah0 @ Z endp; swap_reference_textendspublic? Swap_pointer @ yaxpah0 @ Z; swap_pointer_textsegment_ptr_x $ = 8_ptr_y $ = 12_temp $ =-4? Swap_pointer @ yaxpah0 @ Z proc near; swap_pointer; 11: void swap_pointer (int * ptr_x, int * ptr_y) {vertex, esppushecx; 12: int temp = * ptr_x; moveax, dword ptr _ ptr_x $ [EBP]; [EBP + 8] stores the memory address of X. Do you still remember the eax pressed when swap_pointer is called in the main function? movecx, dword ptr [eax] movdword PTR _ TEMP $ [EBP], ECx; 13: * ptr_x = * ptr_y; movedx, dword ptr _ ptr_x $ [EBP]; it can be understood that edX points to xmoveax, dword ptr _ ptr_y $ [EBP], and eax points to ymovecx, Dword ptr [eax] movdword PTR [edX], ECx; values are indirectly assigned, and the results directly affect the values of the original X and Y; 14: * ptr_y = temp; movedx, dword ptr _ ptr_y $ [EBP] moveax, dword ptr _ TEMP $ [EBP] movdword PTR [edX], eax; 15: return; 16:} movesp, ebppopebpret0? Swap_pointer @ yaxpah0 @ Z endp; bytes $ =-4_y $ =-8_mainproc near; 18: int main () {pushebpmovebp, espsubesp, 8; 19: int x = 4; movdword PTR _ x $ [EBP], 4; 20: int y = 9; movdword PTR _ y $ [EBP], 9; 21: swap_reference (x, y); leaeax, dword ptr _ y $ [EBP]; obtain y's memory address pusheaxleaecx, dword ptr _ x $ [EBP]; obtain X's memory address pushecxcall? Swap_reference @ yaxaah0 @ Z; press the returned address RET into the stack when calling. ESP minus 4 addesp, 8; 22: swap_pointer (& X, & Y); leaedx, dword ptr _ y $ [EBP]; obtain y's memory address pushedxleaeax, dword ptr _ x $ [EBP]; obtain X's memory address pusheaxcall? Swap_pointer @ yaxpah0 @ Z; press the returned address RET into the stack when calling. ESP minus 4 addesp, 8; 23: Return 0; xoreax, eax; 24:} movesp, ebppopebpret0_mainendp_textendsend

Here, we can see that both the reference and pointer perform the exchange value operation through indirect operations. In the simplest case, the number of value assignment operations for the objects to be bound to is twice. The first time the address of the object is retrieved from the memory of the referenced object, the memory of the specified address (the object to be bound) is used) operations directly affect objects, such:

// Reference_Pointer_Local#include <cstdio>int main() {  int  a = 100;  int& ref = a;  ref = 5;  return 0;}

Its assembly code:

Public_main_textsegment_a $ =-4_ref $ =-8_mainproc near; 4: int main () {pushebpmovebp, espsubesp, 8; 5: int A = 100; movdword PTR _ A $ [EBP], 100; [ebp-4] is the address of a; 6: Int & ref = A; leaeax, dword ptr _ A $ [EBP] movdword PTR _ ref $ [EBP], eax; [ebp-8] is the ref memory address, saving the address of a; 7: ref = 5; movecx, dword ptr _ ref $ [EBP]; first, extract the address movdword PTR [ECx] and 5 of A from the ref memory. Then, perform indirect operations on a, which directly affects the value of.

As for the pointer, You can explore it by yourself. Its working mechanism is similar.


4. Differences between references and pointers

Both references and pointers directly affect the original object through indirect means.

1. for pointers, the pointer can not point to any object (pointing to null), but the reference is bound to an object after initialization. The reference must be initialized at the time of definition, because the reference itself cannot be bound with a Null Object

2. to some extent, for example, in testing, the referenced code is more efficient than the pointer, because the reference is always bound to an object, the pointer is not the case (this is taken from "more effective C ++" item M1)

3. after the custom object is referenced, it is bound to an object "from the End". Any attempt to bind it to other objects is forbidden, and the pointer is quite flexible, it can change the object it points to. In the pointer category, there is no "absolute binding"
In general, if you need to change the object to be pointed at any time, you should use a pointer instead of a reference. If you want to always point to an object and the link is "from one end, then you should use references instead of pointers.

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.