C / C ++ pointer and the difference between a reference

Why C / C ++ language using pointers?

The answer: ① on the one hand, every programming language using the pointer. Than C / C ++ pointer.

Every programming language using the pointer. C ++ pointer exposed to the user (programmer), while Java and C # language such as the pointer hidden.

“Everything uses pointers. C++ just exposes them rather than hiding them,”

It's easier to give someone an address to your home than to give a copy of your home to everyone.

② hand

Advantages and necessity of using pointers:

  • Pointer can effectively shows the data structure;
  • Dynamically allocates memory to achieve free memory management;
  • String can be more convenient to use;
  • Convenient and efficient use of the array
  • Pointer directly to store address data relating to, for example: value passed as good as address transfer efficiency, because the value passed to start the argument of the address takes a value, and then assigned to the parameter is substituted into the function calculation; and the pointer put parameter address directly to the solid parameter address, using data taken directly, efficiency, particularly in the case where the frequency assignment and the like (note:! parameter changes will affect the value of the argument)

References and pointers What is the difference?

Nature: the reference is an alias, the address pointer is specific:

① From the face of it, it can change the value pointer pointing at runtime, and references and once after an object is bound not change. This sentence can be understood as: pointer can be reassigned to point to a different object. But it is always a reference point in the initialization when the specified object can not be changed later, but the specified object whose contents can be changed.
from memory allocation point of view, the program allocates memory area pointer variable, rather than allocating memory for the reference area, since reference must be initialized when the statement, which points to an object that already exists. Reference can not point to a null value.
Note: The standard does not specify whether or not to take up memory references and did not provide specific references to how to achieve, with the specific compiler http://bbs.csdn.net/topics/320095541
③ viewed from the compiler program at compile time, respectively, and add a reference pointer to the symbol table , the symbol table is recorded on the variable name and a variable corresponding to the address. Pointer variable in the symbol table corresponding to the address value of the pointer variable address value, and a reference in the symbol table corresponding to the address value of the address value of the referenced object . After generating a symbol table will not change, thus changing the pointer can point to an object (the value of the pointer variable can be changed), and the reference object can not be changed. This is done using pointers and unsafe use of the main reasons cited safety. In a sense, reference may be regarded as a pointer can not be changed .
④ The facts point to a null reference does not exist, which means using a reference code more efficient than using a pointer to. Because before using references you do not need to test its legality. Instead, the pointer should always be tested against it empty.
⑤ In theory, there is no limit to the number of stages pointer, but can only be a reference. As follows:
  int ** p1; // legal. Pointer to a pointer
  int * & p2; // legal. A reference pointer to
  int & * p3; // illegal. Pointer to a reference is illegal
  int && p4; // illegal. Pointing cited references are illegal
  Note that the above method is read from left to right

 

Source: < http://www.tc5u.com/cpp/2400451.htm >

 

 

In plain words to the following outline:

  • Pointer - For a type T, T * T is a pointer type, that is a variable of type T * can hold the address of an object T, and T can add some type qualifiers, such as const, volatile and so on. Shown below, the pointer shown meanings:

  • Reference - the object reference is an alias, mainly for function arguments and return value types, reference symbol X represents X & type. Shown below, cited meanings shown:

First, reference may not be empty, but the pointer can be empty. Front also said the object reference is an alias, reference is empty - the object does not exist, how could there be an alias! Therefore, the definition of a reference time, must be initialized, is not initialize even if the compiler will pass (compile-time error), however. So if you have a variable is used to point to another object, but it may be empty, then you should use a pointer; if the variable always point to an object, ie, your design does not allow variable is empty, then you should use references.

Note: Because the pointer can not point to any object, you must do before using the pointer sentenced to air operations, but do not have references .

 

Second, the reference point can not be changed, subject to a " till death "; however, the pointer may change point , and point to other objects. Description: Although the reference point can not be changed, but you can change the contents of the initialization of the object.

In terms of operation, for example, +, operation of the direct response to the reference object points, instead of changing points; the operation of the pointer, the pointer to the next object will instead change the contents of the referent.

 

Again, the reference size is the size of the variable pointed to, just as an alias reference only; the pointer is a pointer (address) the size itself, the 32-bit system, generally 4 bytes.

 

Finally, reference is safer than pointers. Due to the absence of a null reference, and reference is once initialized to point to an object, it can not be changed to another reference object, therefore reference is safe. For pointers, it may point to other objects at any time, and may not be initialized, or is NULL, it is unsafe. Although we can not change the const pointer points, but there are still a null pointer, and it is possible to produce wild pointer (ie, multiple pointer to a block of memory, free fall after a pointer, the other pointer became a wild pointer ).

 

In short, can be attributed "to point to a memory pointer, its content is referred to a memory address; and references a block of memory is an alias, a reference point is not changed ."

Special const

Why mention it const keyword? Because const reference pointer is defined and differentiated:

Const pointer const reference VS

 

constant pointer : a pointer constant, before adding const pointer type definition statement, the object pointed to represents a constant.

Defined constant pointer pointing pointer limited only indirect access operation, and not a predetermined operation of a predetermined value of the pointer itself.

 Constant pointer definition of "const int * pointer = & a" tells the compiler, * pointer is constant, not the value of the left * pointer operation.

 

constant reference : the constant point of reference in the type references defined before the statement added const, represents the object pointed to is constant. The same can not be with the pointer variable references to the re-assignment.

 

Constant reference pointer constant VS

 

Before pointer pointer name defined statement added const, it represents the pointer itself is constant. In must be initialized when defining pointer constant ! This is a reference to the inherent properties without the use of const.

 

指针常量定义"int* const pointer=&b"告诉编译器,pointer(地址)是常量,不能作为左值进行操作,但是允许修改间接访问值,即*pointer(地址所指向内存的值)可以修改。

 

常量指针常量VS常量引用常量

 

常量指针常量:指向常量的指针常量,可以定义一个指向常量的指针常量,它必须在定义时初始化。

定义"const int* const pointer=&c"

告诉编译器,pointer和*pointer都是常量,他们都不能作为左值进行操作。

 

而不存在所谓的"常量引用常量",因为引用变量就是引用常量。C++不区分变量的const引用和const变量的引用。程序决不能给引用本身重新赋值,使他指向另一个变量,因此引用总是const的。如果对引用应用关键字const,起作用就是使其目标称为const变量。即

没有:const double const& a=1;

只有const double& a=1;

  1. double b=1;
  2. constdouble& a=b;
  3. b=2;//正确
  4. a=3;//出错error: assignment of read-only reference `a'

 

 

 

总结:有一个规则可以很好的区分const是修饰指针,还是修饰指针指向的数据——画一条垂直穿过指针声明的星号(*),如果const出现在线的左边指针指向的数据为常量;如果const出现在右边指针本身为常量。而引用本身就是常量,即不可以改变指向。

 

指针传递和引用传递

为了更好的理解指针和引用,下面介绍一下指针传递和引用传递。当指针和引用作为函数的参数是如何传值的呢?

  • 指针传递参数本质上是值传递的方式,它所传递的是一个地址值。值传递过程中,被调函数的形式参数作为被调函数的局部变量处理,即在栈中开辟了内存空间以存放由主调函数放进来的实参的值,从而成为了实参的一个副本。值传递的特点是被调函数对形式参数的任何操作都是作为局部变量进行,不会影响主调函数的实参变量的值。
  • 引用传递过程中,被调函数的形式参数也作为局部变量在栈中开辟了内存空间,但是这时存放的是由主调函数放进来的实参变量的地址。被调函数对形参的任何操作都被处理成间接寻址,即通过栈中存放的地址访问主调函数中的实参变量。正因为如此,被调函数对形参做的任何操作都影响了主调函数中的实参变量。

引用传递和指针传递是不同的,虽然它们都是在被调函数栈空间上的一个局部变量,但是任何对于引用参数的处理都会通过一个间接寻址的方式操作到主调函数中的相关变量。而对于指针传递的参数,如果改变被调函数中的指针地址,它将影响不到主调函数的相关变量。如果想通过指针参数传递来改变主调函数中的相关变量, 那就得使用指向指针的指针,或者指针引用

 

从概念上讲。指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变。

而引用是一个别名,它在逻辑上不是独立的,它的存在具有依附性,所以引用必须在一开始就被初始化,而且其引用的对象在其整个生命周期中是不能被改变的(自始至终只能依附于同一个变量)。

std::thread线程需要引用传递参数时,需要std::ref()支持

 

 最后,总结一下指针和引用的相同点和不同点:

相同点

●都是地址的概念;

指针指向一块内存,它的内容是所指内存的地址;而引用则是某块内存的别名。

不同点

●指针是一个实体,而引用仅是个别名;

●引用只能在定义时被初始化一次,之后不可变;指针可变;引用“从一而终”,指针可以“见异思迁”;

●引用没有const,指针有const,const的指针不可变;

具体指没有int& const a这种形式,而const int& a是有的,前者指引用本身即别名不可以改变,这是当然的,所以不需要这种形式,后者指引用所指的值不可以改变

●引用不能为空,指针可以为空;

●“sizeof 引用”得到的是所指向的变量(对象)的大小,而“sizeof 指针”得到的是指针本身的大小;

●指针和引用的自增(++)运算意义不一样;

●引用是类型安全的,而指针不是 (引用比指针多了类型检查)

 

参考文献:http://bbs.csdn.net/topics/80358667

http://www.guokr.com/post/443914/

http://blog.csdn.net/listening_music/article/details/6921608

http://www.tc5u.com/cpp/2400451.htm

转自:https://www.cnblogs.com/gxcdream/p/4805612.html

补充:

1。引用类型成员变量,不必初始化?必须在构造函数的初始化列表中进行初始化,所指定的变量的生存期一定要长于该类的生存期。

https://blog.csdn.net/lazyq7/article/details/48186291

https://blog.csdn.net/olivia77livy/article/details/79664232

https://blog.csdn.net/DirkNow/article/details/8664298

Guess you like

Origin blog.csdn.net/smartgps2008/article/details/90648015