c++11 指针赋值发现改变了指针的内容

stecdeng 2017-02-13 03:42:19
y->prev = x->prev; //? x值改变了 但是我只是改变指针的值
仅仅是指针赋值 但是却改变了指向的内容
但是如果函数
RightRotate(root, z->prev->prev);
调用改为
RightRotate(root, x);
就没问题 不会改变指针指向的内容
请各位帮忙看看 谢谢


#include <memory>
#include <iostream>

using namespace std;


struct SmartPoint {
int val;
std::shared_ptr<SmartPoint> next;
std::shared_ptr<SmartPoint> prev;
SmartPoint() :val(0), next(nullptr), prev(nullptr) {}
};

void RightRotate(std::shared_ptr<SmartPoint>& root, std::shared_ptr<SmartPoint>& x) {
std::cout << x->val << std::endl;
std::shared_ptr<SmartPoint> y = x->next;
std::cout << x->val << std::endl;
y->prev = x->prev; //? x值改变了 但是我只是改变指针的值
if (x == nullptr) {
std::cout << "error" << std::endl;
}
}

void test2() {
std::shared_ptr<SmartPoint> root(new SmartPoint);
std::shared_ptr<SmartPoint> x = root;
x->val = 3;

std::shared_ptr<SmartPoint> y(new SmartPoint);
y->val = 2;
x->next = y;
y->prev = x;

std::shared_ptr<SmartPoint> z(new SmartPoint);
z->val = 1;
y->next = z;
z->prev = y;

RightRotate(root, z->prev->prev);

}



int main()
{
test2();
return 0;
}
...全文
370 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2017-02-14
  • 打赏
  • 举报
回复
这问题不是刚在另一个帖子 (http://bbs.csdn.net/topics/392085649) 里问过一遍了吗。 y->prev 和 x 引用的对象是同一个对象,它们俩无非提供了对同一个对象的两条访问路径,现在用指针的路径修改了对象,再用引用的路径检查,当然会看到修改后的结果啊。
stecdeng 2017-02-13
  • 打赏
  • 举报
回复
引用 7 楼 yshuise 的回复:
[quote=引用 6 楼 stecdeng 的回复:] [quote=引用 3 楼 yshuise 的回复:] 非要用指针 ======================= std::shared_ptr<SmartPoint>& root = new std::shared_ptr<SmartPoint>(...);
是指引用 然后new一个智能指针的指针????[/quote] 一般都是栈(shared_ptr)对象(传入参数是new)才能发挥RAll、防止异常的功能呀![/quote] 我这是红黑树用智能指针重写时候遇到的问题 为了在论坛上询问 做了简化重现 你这个 一遍是智能指针的引用 一遍指是智能指针的指针 应该是不能编译的 谢谢
yshuise 2017-02-13
  • 打赏
  • 举报
回复
引用 6 楼 stecdeng 的回复:
[quote=引用 3 楼 yshuise 的回复:] 非要用指针 ======================= std::shared_ptr<SmartPoint>& root = new std::shared_ptr<SmartPoint>(...);
是指引用 然后new一个智能指针的指针????[/quote] 一般都是栈(shared_ptr)对象(传入参数是new)才能发挥RAll、防止异常的功能呀!
stecdeng 2017-02-13
  • 打赏
  • 举报
回复
引用 3 楼 yshuise 的回复:
非要用指针 ======================= std::shared_ptr<SmartPoint>& root = new std::shared_ptr<SmartPoint>(...);
是指引用 然后new一个智能指针的指针????
stecdeng 2017-02-13
  • 打赏
  • 举报
回复
引用 4 楼 jianwen0529 的回复:
[quote=引用 2 楼 stecdeng 的回复:] [quote=引用 1 楼 jianwen0529 的回复:] 参数把 & 去掉! 可能是因为&导致智能指针计数错误
去掉就OK 了 但是我跟了源码没看出所以然 所以这里询问下各位 也好了解底层机制么[/quote] 我再仔细看了下,参数使用引用不会造成计数器的错误。 再来去掉&,总的来说逻辑也不合理(对于排序或者其他改变顺序的情况下) y->prev = x->prev; 其中y->prev 实际上是 x(即root),x->prev = nullptr 你这个赋值语句相当于修改 x 的值[/quote] 这是改变指针的数值 不是改变指针指向的内容 但是如果函数 RightRotate(root, z->prev->prev); 调用改为 RightRotate(root, x); 就没问题 不会改变指针指向的内容 X就不会变化
幻夢之葉 2017-02-13
  • 打赏
  • 举报
回复
引用 2 楼 stecdeng 的回复:
[quote=引用 1 楼 jianwen0529 的回复:] 参数把 & 去掉! 可能是因为&导致智能指针计数错误
去掉就OK 了 但是我跟了源码没看出所以然 所以这里询问下各位 也好了解底层机制么[/quote] 我再仔细看了下,参数使用引用不会造成计数器的错误。 再来去掉&,总的来说逻辑也不合理(对于排序或者其他改变顺序的情况下) y->prev = x->prev; 其中y->prev 实际上是 x(即root),x->prev = nullptr 你这个赋值语句相当于修改 x 的值
yshuise 2017-02-13
  • 打赏
  • 举报
回复
非要用指针 ======================= std::shared_ptr<SmartPoint>& root = new std::shared_ptr<SmartPoint>(...);
stecdeng 2017-02-13
  • 打赏
  • 举报
回复
引用 1 楼 jianwen0529 的回复:
参数把 & 去掉! 可能是因为&导致智能指针计数错误
去掉就OK 了 但是我跟了源码没看出所以然 所以这里询问下各位 也好了解底层机制么
幻夢之葉 2017-02-13
  • 打赏
  • 举报
回复
参数把 & 去掉! 可能是因为&导致智能指针计数错误

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧