为什么一个c ++引用被认为比一个指针更安全? [英] Why is a c++ reference considered safer than a pointer?

查看:282
本文介绍了为什么一个c ++引用被认为比一个指针更安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当c ++编译器为引用和指针生成非常类似的汇编代码时,为什么使用引用与指针相比更喜欢(并且更安全)?

When the c++ compiler generates very similar assembler code for a reference and pointer, why is using references preferred (and considered safer) compared to pointers?

  • Difference between pointer variable and reference variable in C++ which discusses the differences between them.

EDIT-1:

我正在查看g ++为这个小程序生成的汇编代码:

I was looking at the assembler code generated by g++ for this small program:

int main(int argc, char* argv[])
{
  int a;
  int &ra = a;
  int *pa = &a;
}


推荐答案

的人已经听到它更安全,然后告诉别人,他们现在也听说它更安全。

It's considered safer because a lot of people have "heard" that it's safer and then told others, who now have also "heard" that it's safer.

没有一个人理解参考将告诉你

Not a single person who understands references will tell you that they're any safer than pointers, they have the same flaws and potential to become invalid.

例如

#include <vector>

int main(void)
{
    std::vector<int> v;
    v.resize(1);

    int& r = v[0];
    r = 5; // ok, reference is valid

    v.resize(1000);
    r = 6; // BOOM!;

    return 0;
}

编辑:因为似乎有些混淆,对于一个对象或绑定到一个内存位置,这里是标准的一段(草案3225, [basic.life] ),它清楚地表明一个引用绑定到存储和可以超过在创建引用时存在的对象:

Since there seems to be some confusion about whether a reference is an alias for an object or bound to a memory location, here's the paragraph from the standard (draft 3225, section [basic.life]) which clearly states that a reference binds to storage and can outlive the object which existed when the reference was created:


如果在对象的生命周期结束后,对象占用被重用或
释放,在原始对象占用的存储位置创建一个新对象,
指向原始对象的指针,引用原始对象的引用,或原始
对象的名称将自动引用新对象,并且一旦新对象的生命周期开始,可以使用
来操作新对象,如果:

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:


  • 新物件的储存空间完全覆盖原始物件占用的储存位置,

  • 对象与原始对象具有相同的类型(忽略顶级cv-qualaliter),并且

  • 原始对象的类型不是常量限定的,如果一个类类型,不包含类型为const或引用类型的任何非静态
    数据成员,并且

  • 原始对象是类型<$的最多派生对象c $ c> T ,并且新对象是类型 T (即它们不是基类子对象)的最大派生对象。 / li>
  • the storage for the new object exactly overlays the storage location which the original object occupied, and
  • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and
  • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and
  • the original object was a most derived object of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

这篇关于为什么一个c ++引用被认为比一个指针更安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆