Differences between references and pointers in C + +

Source: Internet
Author: User
Tags aliases

Here's an overview: pointers--For a type t,t* is a pointer type that points to T, and a t* type of variable can hold the address of a T object, and type T can add some qualifier, such as const, volatile, and so on. See the following figure, the meaning of the pointer:

Reference-Reference is an alias for an object, primarily for function arguments and return value types, and symbol x& represents a reference of type X. See the following figure, the meaning of the reference:

2. The difference between pointers and references first, the reference cannot be null, but the pointer can be empty. As mentioned earlier, references are aliases to objects, and references are empty--objects do not exist, and there can be aliases. So when you define a reference, you must initialize it. So if you have a variable that is used to point to another object, but it may be empty, you should use a pointer; If the variable always points to an object, i.e. your design does not allow the variable to be empty, you should use a reference. In the following illustration, if you define a reference variable, you cannot even compile without initializing it (compile-time error):

A declaration pointer can be without pointing at any object, and for that reason, you must do a null-and-void operation before using the pointer, and the reference is not necessary . Second, a reference cannot change the point of "Death to death" on an object, but the pointer can change points and point to other objects. Note: Although references may not change the point, you can change the contents of the initialization object. For example, for the + + operation, the operation of the reference directly reacts to the object being pointed to, rather than changing the point, and the action on the pointer causes the pointer to point to the next object instead of changing the contents of the object being referred to. See the following code:

#include <iostream>

using namespace Std;

int main (int argc,char** argv)

{

int i=10;

int& ref=i;

ref++;

cout<< "i=" <<i<<endl;

cout<< "ref=" <<ref<<endl;

int j=20;

Ref=j;

ref++;

cout<< "i=" <<i<<endl;

cout<< "ref=" <<ref<<endl;

cout<< "j=" <<j<<endl;

return 0;

}

The + + operation of ref is a direct reaction to the variable referred to, and the value of the reference variable ref "REF=J" does not change the direction of ref, it still points to I instead of J. Of course, the + + operation on ref does not affect J. These changes are pointers, the situation is very different, please experiment. The output results are as follows:

Again, the size of the reference is the size of the variable being pointed to, because the reference is only an alias; the pointer is the size of the pointer itself, 4 bytes. As shown in the following figure:

As you can see from the above , references are more formal than pointers, and using references to content can be used to refer to variable names instead of using * when you define references, and you don't need to use a & address like a pointer. Finally, the reference is more secure than the pointer. Because a null reference does not exist, and once the reference is initialized to point to an object, it cannot be changed to a reference to another object, so the reference is safe. For pointers, it can point to other objects at any time, and can be either uninitialized or null, so it is unsafe. The const pointer cannot change the point, but there is still a null pointer, and it is possible to produce wild pointers (that is, multiple pointers point to a piece of memory, and after a pointer is dropped, the other pointers become wild pointers).

All in all, words and all--their differences can all boil down to "a pointer to a piece of memory, its content is the address of the memory, and a reference is the alias of a block of memory, the reference does not change the point." " 3, the special const

Why do I mention the const keyword here? Because there is a difference between a const pointer and a reference, listen to my one by one coming. Constant pointer vs constant reference

Constant pointer: A pointer to a constant that is a const before the type of the pointer defines the statement, indicating that the object being pointed to is a constant.

The definition of a pointer to a constant restricts the indirect access operation of the pointer, not the operation of the value itself that the pointer points to.

The constant pointer defines "const int* Pointer=&a" to tell the compiler that *pointer is a constant and that *pointer cannot be manipulated as a left value.

Constant reference: a reference to a constant, plus a const before referencing the type of the definition statement, indicating that the object being pointed to is a constant. It's like a pointer. You cannot use a reference to reassign a variable that points to a value.

Pointer constant vs Reference constant

Add a const before the pointer name of the pointer definition statement, indicating that the pointer itself is a constant. You must initialize the pointer constants when defining them. This is a reference to the nature of the attribute, no longer refer to the reference name of the pointer definition statement before adding a const.

The pointer constant defines "int* const pointer=&b" to tell the compiler that pointer is a constant and cannot operate as a left value, but that the indirect access value is allowed to be modified, that is, *pointer can be modified.

Constant pointer constant vs constant reference constant

Constant pointer constant: A pointer constant to a constant that defines a pointer constant that must be initialized when it is defined. Constant pointer constants define "Const int* const POINTER=&C" Tell the compiler that both pointer and *pointer are constants and they cannot operate as left values.

There is no such thing as a "constant reference constant", because the reference variable is the reference constant as mentioned above. C + + does not distinguish between a const reference of a variable and a reference to a const variable. The program must not reassign the reference itself so that he points to another variable, so the reference is always const. If you apply a keyword const to a reference, the effect is to make its target known as a const variable. That is not: const double const& a=1; only the const double& a=1;

Summary: There is a rule that distinguishes whether the const is a modifier pointer or the data that the pointer points to--draw a vertical asterisk (*) that crosses the pointer, and if the const appears to the left of the line, the pointer points to a constant; if the const appears to the right, the pointer itself is a constant. And the reference itself with the sky is constant, that is, can not change the point. 4. Implementation of pointers and references

We use the following simple code to drill down on pointers and references:

#include <iostream>

using namespace Std;

int main (int argc, char** argv)

{

int i=1;

int& ref=i;

int x=ref;

cout<< "x is" <<x<<endl;

ref=2;

int* p=&i;

cout<< "ref =" <<ref<< ", i =" <<i<<endl;

}

The code above is compiled with g++ test.c and then disassembled objdump-d a.outto get a section of the assembly code for the main function as follows:

08048714 <main>:

8048714:55 Push%EBP

8048715:89 e5 mov%esp,%ebp

8048717:83 e4 F0 and $0xfffffff0,%esp//as parameters for main function argc, argv reserved position

804871a:56 Push%esi

804871b:53 Push%EBX

804871c:83 EC Sub $0x28,%esp

804871F:C7 1c MOVL $0x1,0x1c (%ESP)//Save 0x1 to ESP register, int i=1

8048726:00

8048727:8d 1c Lea 0X1C (%ESP),%eax//the address of variable I in ESP registers to EAX

804872b:89%eax,0x18 (%ESP)//To pass the contents of the register EAX (address of I) to the variable ref in the register, that is, int& ref=i

804872f:8b mov 0x18 (%ESP)%eax//to pass ref in the Register ESP to EAX, the address of I

8048733:8B mov (%EAX),%eax//with the value in the register EAX as the address, take out the value to eax 8048735:89-mov%eax,0x14 (%ESP)//The value in the register EAX Pass to the X in the register ESP, that is, the X=ref

8048739:c7 movl $0x8048900,0x4 (%ESP)

8048740:08

8048741:c7 A0 movl $0x804a040, (%ESP)

8048748:e8 CB FE FF FF call 8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>

804874d:8b mov 0x14 (%esp),%edx

8048751:89 mov%edx,0x4 (%ESP)

8048755:89 mov%eax, (%ESP)

8048758:e8 5b FE FF FF call 80485B8 <_ZNSolsEi@plt>

804875d:c7 movl $0x8048638,0x4 (%ESP)

8048764:08

8048765:89 mov%eax, (%ESP)

8048768:e8 BB fe ff FF call 8048628 <_znsolsepfrsos_e@plt>//from 8048739~8048768 These lines are executed "cout<<" x is "<& lt;x<<endl; "

804876d:8b mov 0x18 (%ESP),%eax//to the Register ESP in the ref to EAX

8048771:c7 movl $0x2, (%EAX)//Save 0x2 in EAX register

8048777:8d 1c Lea 0X1C (%ESP),%eax//the address of variable I in ESP registers to EAX

804877b:89%eax,0x10 (%ESP)//Transfer the contents of the register eax (that is, the address of I) to P in the Register ESP

804877f:8b 5c 1c mov 0x1c (%ESP),%EBX

8048783:8b mov 0x18 (%esp),%eax

8048787:8b mov (%eax),%esi

8048789:c7 movl $0x8048906,0x4 (%ESP)

8048790:08

8048791:c7 A0 movl $0x804a040, (%ESP)

8048798:e8 7b FE FF FF call 8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>

804879d:89 mov%esi,0x4 (%ESP)

80487a1:89 mov%eax, (%ESP)

80487a4:e8 0f FE FF FF call 80485B8 <_ZNSolsEi@plt>

80487a9:c7 0d movl $0x804890d,0x4 (%ESP)

80487b0:08

80487b1:89 mov%eax, (%ESP)

80487b4:e8 5f FE FF FF call 8048618 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>

80487b9:89 5c mov%ebx,0x4 (%ESP)

80487bd:89 mov%eax, (%ESP)

80487c0:e8 f3 fd ff call 80485b8 <_ZNSolsEi@plt>

80487c5:c7 movl $0x8048638,0x4 (%ESP)

80487cc:08

80487cd:89 mov%eax, (%ESP)

80487d0:e8 FE FF FF call 8048628 <_znsolsepfrsos_e@plt>//These lines are executed "cou

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.