Differences between references and Pointers-general Linux technology-Linux programming and kernel information. For more information, see the following. A reference is equivalent to * p;
The referenced definition is object & a = object ();
The pointer is object * a = & object ();
The referenced memory cannot be deleted. Generally, it is a stack variable.
Use references whenever possible.
When a reference is used as a parameter, a constant cannot be passed to it. It must be a variable name.
Add (int & a, int & B)
Add (4, 4) // invalid
Int a = B = 4;
Add (a, B) // OK
When they are returned values, they are completely different. The so-called return reference does not refer to obect & getObject (); instead, it refers to object * getObject () {return & object ;}
The C ++ instructor said, "The reference is actually an alias, Which is exactly equivalent to the referenced object." However, I still have this question: "What is the equivalent meaning? Does it mean that the two are directly using the same memory location in the underlying implementation? "For example, the following code:
Int main ()
...{
Int a = 100;
Int & B =;
Int * c = &;
B = 99;
A = 88;
B = 77;
* C = 66;
Return 0;
}
After compilation, it becomes like this:
Main:
Leal 4 (% esp), % ecx
Andl $-16, % esp
Pushl-4 (% ecx)
Pushl % ebp
Movl % esp, % ebp
Pushl % ecx
Subl $16, % esp
. LCFI5:
Movl $100,-16 (% ebp); ebp-16 is the address of variable
Leal-16 (% ebp), % eax
Movl % eax,-12 (% ebp); place the address of a at the ebp-12 address
Leal-16 (% ebp), % eax
Movl % eax,-8 (% ebp); The ebp-8 is the pointer c variable, this sentence is to assign a value to the pointer c
Movl-12 (% ebp), % eax; before assigning a value to reference B, take the address of a with a ebp-12 to eax
Movl $99, (% eax); B = 99
Movl $88,-16 (% ebp)
Movl-12 (% ebp), % eax; here, we assign a value to reference B, which is the same as the previous one.
Movl $77, (% eax)
Movl-8 (% ebp), % eax; obtain the c address before assigning values to * c.
Movl $66, (% eax); run * c = 66 here
Movl $0, % eax
Addl $16, % esp
Popl % ecx
Popl % ebp
Leal-4 (% ecx), % esp
Ret
It can be seen that, in actual implementation, a temporary space (stack space) will be applied for to store the address of the referenced object. when a referenced variable is used, the referenced object is indirectly addressable. from the perspective of assembly code, * c is indeed the same as B's assembly code in use. It takes the address first and then saves the value to this address. the difference is that at the c ++ level, as a reference type variable such as B, values can only be assigned at initialization and can only be assigned once.
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