--by KAROTTC
Analyze the reference type in C + + (for example: int &r = A; ), r does the variable occupy memory space? Whether and int *p = &a; Use the p same memory space as the variables in the
This article will give an answer.
Look directly at a simple example:
#include <iostream>usingnamespace std; int Main (void) { int6; int &r = A; int *p = &A; int x = R; return 0 ;}
Then we passed g++ testref. CPP -o testref-g after compiling, use GDB to load it, and see a , r p x ,, number of addresses:
The address is a 0x7fffffffe208 , with the address character to get the r address, the value and a is the same, in fact, the & symbol is not able to get to the address of the reference type variable, because the reference type of the variable itself is just another object alias, in a perceptual way to describe, Is that it is just a name, any action on it is equivalent to the operation of another object, so this take address operation is the same.
However, we continue to look down, p the address is 0x7fffffffe218 , and a the address just 16 bytes, followed by the address x is 0x7fffffffe20c , this address is exactly a the address 0x7fffffffe208 + 4 , but the a int type, the variable itself occupies 4 Byte, normally, x the address should be p the address 0x7fffffffe218 + 8 , here +8 is because my machine is a 64-bit machine, so the pointer type occupies 8 bytes. Now this should be the compiler to do the optimization, put the x back of the a next, the same, x the starting address of a variable should be 0x7fffffffe20c + 4 = 0x7fffffffe210 , the address and the p exact difference between 8 bytes, that is, the address of a pointer variable , so the answer is obvious.
That is, we can now guess that r it is taking up memory space and occupying the same size as the pointer variable. But we do not see r the actual address of GDB, so this can only be guessed, just seemingly reasonable speculation.
To prove this speculation, we continue to disassemble the executable file to see: objdump -d testref the results are as follows:
00000000004006CD <main>: 4006CD: -Push%RBP 4006ce: - theE5 mov%rsp,%RBP 4006d1:c7 $E8 . xx xx xxMOVL $0x6,-0x18(%RBP) #intA =6; Address of a0x184006d8: -8d $E8 Lea-0x18(%RBP),%Rax 4006DC: - the $F0 mov%rax,-0x10(%RBP) #int&r = A; Address of R0x104006E0: -8d $E8 Lea-0x18(%RBP),%Rax 4006e4: - the $F8 mov%rax,-0x8(%RBP) #int*p = &a; Address of P0x84006e8: -8b $F0 mov-0x10(%RBP),%Rax # The following two lines are prepared for the subsequent assignment 4006ec:8bxxMOV (%rax),%eax 4006ee: the $EC mov%eax,-0x14(%RBP) #intx = r; Address of X0x144006f1:b8xx xx xx xxMOV $0x0,%eax 4006f6:5d Pop%RBP 4006f7:c3 retq
I wrote the correspondence in the comments above.
Therefore, it is now possible to conclude that variables of reference types consume memory space, and the size of the memory space used is the same as the size of the pointer type. as can be seen from the assembly code above, although the reference is an alias of an object, at the assembly level, the pointer is the same.
"This article is starting at: http://www.karottc.com/blog/2015/07/29/cpp-reference/"
2015.07.29
Does the C + + reference type have a variable that takes up no memory space?