简体   繁体   English

引用的内存分配

[英]Memory allocation for references

Read a lot of differences between pointers & references . 阅读pointersreferences之间的许多差异。

Here is a brief description of what i learned. 以下是我学到的内容的简要说明。

1 . 1 Memory is allocated when a pointer is defined. 定义指针时分配内存。 A reference however, is a name alias & hence no memory is allocated for it( Is it correct? ). 但是,引用是名称别名,因此没有为它分配内存( Is it correct? )。

2 . 2 Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object. 必须在定义时初始化引用,因为引用是使用常量指针实现的,因此不能指向另一个对象。 A pointer however, is not necessary to be initialized at the time of definition & hence can also be changed to point to some other object. 然而,指针在定义时不需要初始化,因此也可以改变为指向某个其他对象。

3 . 3 A reference automatically gets de-referenced. 引用自动被取消引用。 When you write cout << p ; 当你写cout << p ; It is automatically de-referenced by the compiler & treated as cout << *p ; 它被编译器自动解除引用并被视为cout << *p ; by the compiler. 由编译器。

Here, p is the reference. 这里,p是参考。

  1. A reference to a reference is not possible.Whenever, you declare a reference to a reference, its actually the reference to the same variable. 无法引用引用。无论何时,您声明对引用的引用,它实际上是对同一变量的引用。 eg 例如

     int i; int &r1=i; int &r2=r1; <-------------------2 

Compiler interprets the statement 2 as: 编译器将语句2解释为:
int &r2=(*r1) int&r2 =(* r1)
and (*r1) is nothing but the variable i itself. 和(* r1)只不过是我自己的变量。

A pointer to a pointer is however possible. 然而,指向指针的指针是可能的。

5 . 5 Array of pointer is possible while array of references is not possible(Why?). 指针数组是可能的,而引用数组是不可能的(为什么?)。

6 . 6 Address of pointer is possible. 指针的地址是可能的。 Address of reference is not possible. 无法提供参考地址。 It gives of the address of the variable. 它给出了变量的地址。

7 . 7 There are situations where you are bound to use references.You cannot use pointers there. 在某些情况下,您必须使用引用。您不能在那里使用指针。 Consider the below example: 考虑以下示例:

A a=b+c; A a = b + c;

Where a,b,c are objects of class A. The operator '+' is overloaded as follows: 其中a,b,c是A类的对象。运算符'+'按如下方式重载:

const A& operator+(const A& o)
{
     return A(i+o.i);
}

See sample code here: http://ideone.com/Q0pE1 请参阅示例代码: http//ideone.com/Q0pE1

Here the reference in the argument list is used to save the memory footprints. 这里参数列表中的引用用于保存内存占用。
You cannot use pointer in the argument list as you are bound to pass the address of object in the operator function. 您不能在参数列表中使用指针,因为您必须在运算符函数中传递对象的地址。
A a=&b + &c; A =&b +&c;
However, if pointer is used in the parameter list, then we will end up adding the addresses rather than object itself. 但是,如果在参数列表中使用指针,那么我们最终将添加地址而不是对象本身。

I want to know that is there any other point that i am missing? 我想知道我还缺少其他任何一点吗?

When should one go for pointer & when for reference? 什么时候应该去指针和什么时候参考?

Memory is allocated when a pointer is defined. 定义指针时分配内存。 A reference however, is a name alias & hence no memory is allocated for it 但是,引用是名称别名,因此没有为其分配内存

What do you mean by "memory is allocated?" 什么是“分配内存?” If you mean a heap allocation, as with new or malloc or whatever, no : 如果你的意思是堆分配,就像newmalloc或其他什么一样, 没有

int val = 5;
int *pVal = &val; //No dynamic memory allocation.

A pointer has a size , in the same way that int has a size. 指针的大小int具有大小的方式相同。 But that's different from an "allocation". 但这与“分配”不同。

Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object. 必须在定义时初始化引用,因为引用是使用常量指针实现的,因此不能指向另一个对象。

No, a reference is bound at initialization time because that's how references work . 不,引用在初始化时绑定,因为引用的工作方式 They are references to objects. 它们是对象的引用。 The language states that it is impossible for them to not be bound, and it is impossible for their binding to change later. 该语言指出,他们不可能不被束缚,他们的约束力不可能在以后改变。 Therefore, it is necessary that references are bound when they are initialized. 因此,在初始化引用时必须绑定引用。

How a compiler implements references is entirely irrelevant. 编译器如何实现引用完全无关紧要。

A reference automatically gets de-referenced. 引用自动被取消引用。

No. There is nothing to de-reference. 没有没有什么可以去参考。 A reference is merely another name for an already existing object. 引用仅是已存在对象的另一个名称。 That's all. 就这样。

Array of pointer is possible while array of references is not possible(Why?). 指针数组是可能的,而引用数组是不可能的(为什么?)。

Because a reference has to be bound when it is initialized. 因为在初始化时必须绑定引用。 And it's not possible to give each member of an array a separate object to be bound to. 并且不可能为数组的每个成员分配一个单独的对象。 Thus, you would need some step between the creation of the array and the binding of the reference. 因此,在创建数组和引用的绑定之间需要一些步骤。 Which is not allowed. 这是不允许的。

Address of pointer is possible. 指针的地址是可能的。 Address of reference is not possible. 无法提供参考地址。 It gives of the address of the variable. 它给出了变量的地址。

A reference is another name for an already existing object. 引用是已存在对象的另一个名称。 You can't get the address of a name ; 你不能得到一个名字的地址; you can only get the address of an object. 你只能得到一个对象的地址。

There are situations where you are bound to use references. 在某些情况下,您必须使用引用。

There's nothing stopping you from overloading operator+ for pointers to the types: 没有什么可以阻止你重载operator+指向类型的指针:

A operator+(const A *lhs, const A *rhs) {...}

Of course, this is a non-member function. 当然,这是一个非会员功能。

Oh, and as a bonus: 哦,作为奖励:

const A& operator+(const A& o)
{
     return A(i+o.i);
}

This is broken. 这已破了。 You're returning a const& to a temporary you create. 你正在返回一个const&你创建的临时。 You should be returning it by value. 你应该按价值归还它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM