两个指针指向相同的地址 [英] Two pointers pointing to the same address

查看:756
本文介绍了两个指针指向相同的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

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.

这篇关于两个指针指向相同的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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