C++ 两个指针指向同一个地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10370653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:58:06  来源:igfitidea点击:

Two pointers pointing to the same address

c++cpointers

提问by Daneshju M

What happens when two pointers are pointing to the same address? Is this going to cause a security problem?

当两个指针指向同一个地址时会发生什么?这会导致安全问题吗?

回答by Luchian Grigore

The fact itself is ok, but you'll run into undefined behavior if you call deleteon one of the pointers and attempt to use the other afterwards:

事实本身是可以的,但是如果您调用delete其中一个指针并随后尝试使用另一个指针,则会遇到未定义的行为:

int* x = new int(5);
int* y = x;
delete x;
//y is a dangling pointer

If you run into a situation where you have to use multiple pointers to the same memory address, you should look into smart pointers.

如果遇到必须使用多个指向同一内存地址的指针的情况,则应查看智能指针

回答by Apples

It is safe to have more than one pointer to the same address, but make sure that you know that if the memory is deleted using deleteor if the original variable goes out of scope, further access to it will be undefined.

拥有多个指向同一地址的指针是安全的,但请确保您知道如果使用删除内存delete或原始变量超出范围,将无法定义对其的进一步访问。

回答by Abhijit

Depends:

要看:

For Classic (Non-Smart) Pointers: You can have more than one pointers to point to the same memory location but manipulating the location would be percolated across all the pointers. Deleting/Freeing the storage of one pointer would cause undefined behavior when other pointers are being used. Note the best practice is to make a pointer NULL once the storage is freed to prevent double delete and when using multiple pointers this is not pragmatically plausible.

对于经典(非智能)指针:您可以有多个指针指向同一个内存位置,但操作该位置将渗透到所有指针中。当正在使用其他指针时,删除/释放一个指针的存储将导致未定义的行为。请注意,最佳做法是在释放存储空间后将指针设为 NULL 以防止双重删除,并且在使用多个指针时,这在实际中是不合理的。

Smart Pointers

智能指针

Auto_Pointers(C++98): C++ template class implementation provided a mechanism to allow one and only one pointer to point to one memory location. Generally as these pointers are implemented on stack as object, when they come out of scope the address would automatically be freed. But copy assignment of one pointer to another would make the other unusable.

Auto_Pointers(C++98):C++ 模板类实现提供了一种机制,允许一个且仅一个指针指向一个内存位置。通常,由于这些指针作为对象在堆栈上实现,因此当它们超出范围时,地址将自动释放。但是将一个指针复制到另一个指针会使另一个无法使用。

Shared_Pointers(C++2003 TR1): Boost library provided shared pointers which has a reference count which determines when the object could be freed. This is better than auto_ptrs as you can have multiple usable pointers sharing the same memory location.

Shared_Pointers(C++2003 TR1):Boost 库提供了共享指针,它有一个引用计数,用于确定何时可以释放对象。这比 auto_ptrs 更好,因为您可以有多个可用的指针共享相同的内存位置。

Unique_Pointers(C++11): Similar to Auto_pointers, but inherit the concept of transfer of ownership where assignment and copy constructor are not exposed. Instead a move method is implemented to make it more clear what the intention is.

Unique_Pointers(C++11):类似于 Auto_pointers,但继承了所有权转移的概念,其中不公开赋值和复制构造函数。相反,实现了一个 move 方法,以使其更清楚意图是什么。

回答by ouah

If both pointers are of the same type, there is no issue:

如果两个指针的类型相同,则没有问题:

int a = 42;
int *p = &a
int *q = p;
*p = 3145;   // no problem, a and *q are now also equal 3145

If both pointers are of different types (with the exception of char *) and point to the same object, dereferencing one of the pointer is undefined behavior.

如果两个指针的类型不同(除了char *)并指向同一个对象,则取消引用其中一个指针是未定义的行为。

float a = 42.0f;
float *p = &a;
int *q = (int *) p;  // we assume pointer is correctly aligned
*q = 0;              // undefined behavior, it breaks aliasing rules

These rules are known as the C pointers aliasing rules. You can find a list of the C aliasing rules in n1570.pdfin paragraph 6.5p7.

这些规则称为 C 指针别名规则。你可以找到的C走样规则中的列表n1570.pdf段6.5p7。

回答by Arjun

Both the objects are pointing to same heap address.

两个对象都指向相同的堆地址。

At the time when you will de-allocate memory then it may cause problems to other objects pointing to same address.

当您取消分配内存时,可能会导致指向同一地址的其他对象出现问题。