Do C ++ variables of the reference type occupy no memory space ?, Memory occupied by Variables
-- By karottc
Analyze the reference types in C ++ (for example, int & r = ;)rDoes the variable occupy memory? Whether it is equal to int * p = &;pWhat if variables occupy the same memory space?
This article provides the answer.
Let's look at a simple example:
#include <iostream>using namespace std;int main(void){ int a = 6; int &r = a; int *p = &a; int x = r; return 0;}
Next, we compile it through g ++ testref. cpp-o testref-g and use gdb to load it. Let's take a look.a,r,p,xRespectively:
MediumaIs0x7fffffffe208, Get with the address characterrAddress, value, andaIs the same, in fact, use&The symbol cannot obtain the address of the referenced type variable, because the referenced type variable itself is only the alias of another object and can be described in a perceptible way, that is, it is just a name, any operation on it is equivalent to an operation on another object, so the same operation is performed on this address.
However, let's look down,pThe address is0x7fffffffe218, AndaThe address is 16 bytes different.xThe address is0x7fffffffe20cThe address is exactlyaAddress0x7fffffffe208 + 4, AndaYesintVariable occupies 4 bytes. Normally,xThe address must bepAddress0x7fffffffe218 + 8, Here+8Because my machine is a 64-bit machine, the pointer type occupies 8 bytes. The current situation should be caused by the optimization of the compiler.xToa,xThe starting address of the next variable is0x7fffffffe20c + 4=0x7fffffffe210, This address andpThe difference is exactly 8 bytes, that is, the address of a pointer variable, so the answer is obvious.
That is, we can guess now,rMemory usage, and the occupied size is the same as the pointer variable size. But we can't see it using gdb.rThe actual address, so this can only be a guess, just a reasonable guess.
To prove this guess, we continue to disassemble the executable file to see it:objdump -d testrefThe result is as follows:
201710000004006cd <main>: 4006cd: 55 push % rbp 4006ce: 48 89 e5 mov % rsp, % rbp 4006d1: c7 45 e8 06 00 00 00 movl $0x6, -0x18 (% rbp) # int a = 6; address 0x18 4006d8: 48 8d 45 e8 lea-0x18 (% rbp), % rax 4006dc: 48 89 45 f0 mov % rax,-0x10 (% rbp) # int & r = a; r address 0x10 4006e0: 48 8d 45 e8 lea-0x18 (% rbp), % rax 4006e4: 48 89 45 f8 mov % rax,-0x8 (% rbp) # int * p = & a; p address 0x8 4006e8: 48 8b 45 f0 mov-0x10 (% rbp ), % rax # The following two rows are prepared for the subsequent assignment 4006ec: 8b 00 mov (% rax), % eax 4006ee: 89 45 ec mov % eax, -0x14 (% rbp) # int x = r; x address 0x14 4006f1: b8 00 00 00 00 mov $0x0, % eax 4006f6: 5d pop % rbp 4006f7: c3 retq
I wrote the corresponding content in the above comments.
Therefore, we can conclude that variables of the reference type occupy the memory space, and the size of the memory space is the same as that of the pointer type. From the assembly code above, we can see that although the reference is an object alias, it is the same as the pointer at the Assembly level.
[This article was first published at: http://www.karottc.com/blog/2015/07/29/cpp-reference /]
2015.07.29