这里的空悬指针哪里来的?

blijiang 2009-02-11 02:02:41
如下代码:
const Thing* last_accessed_thing;
void access(const Thing& t) {
// ...
last_accessed_thing = &t;
}

extern Thing get_a_thing();
void f() {
access(get_a_thing());
}
请问为何last_accessed_thing会变成空悬指针?为何get_a_thing会产生临时对象并被access()摧毁?
按照我的理解,access参数是个引用参数,直接取传入参数的地址,并不会产生临时对象。这样t的地址就是get_a_thing()的地址,last_accessed_thing就是get_a_thing()的地址,然而get_a_thing()并没有产生临时对象阿。
...全文
184 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2009-02-12
  • 打赏
  • 举报
回复
No uniform answer.
Some said no temporary variable created, others said temporary objects created.
Some said the reason is for extern get_a_thing(), while others didn't mention this.
hmt.sysfixer.net 2009-02-12
  • 打赏
  • 举报
回复
纠正一下:

如下代码:
const Thing* last_accessed_thing;
void access(const Thing& t) {
// ...
last_accessed_thing = &t;
}

extern Thing get_a_thing();
void f() {
access(get_a_thing());
}

这段代码确实不会产生临时对象,last_accessed_thing成为野指针的原因在于get_a_thing()这个函数返回的是局部对象,

Thing get_a_thing()
{
Thing thing;
return thing;
}

last_accessed_thing指向的是局部对象thing的地址。
yndfcd 2009-02-12
  • 打赏
  • 举报
回复
虽然const引用会延长临时对象的生存期.但是只是延长到与const 引用一样长.

这里的const引用在access(get_a_thing());结束之后生存期也结束了.因此该临时对象析构了.
old-six-programmer 2009-02-12
  • 打赏
  • 举报
回复
怎么突然整e文了
arong1234 2009-02-11
  • 打赏
  • 举报
回复
hmt.sysfixer.net 2009-02-11
  • 打赏
  • 举报
回复
如下代码:
const Thing* last_accessed_thing;
void access(const Thing& t) {
// ...
last_accessed_thing = &t;
}

extern Thing get_a_thing();
void f() {
access(get_a_thing());
}

关于上述代码可以分析一下:
首先,根据extern Thing get_a_thing(); 这个声明知道是值返回,那肯定会产生临时对象;
其次,临时对象的生命周期在这个access(get_a_thing()); 表达式中,等运行到下一行代码时,临时对象就会析构,
而last_accessed_thing指向的是临时对象的指针,故等临时变量失去作用域时肯定就会编程“野指针”;
lzh9955 2009-02-11
  • 打赏
  • 举报
回复
oooooo
dongpy 2009-02-11
  • 打赏
  • 举报
回复
const Thing* last_accessed_thing;
void access(const Thing& t) {
// ...
last_accessed_thing = &t;
}

extern Thing get_a_thing();
void f() {
access(get_a_thing());
}
================================================
get_a_thing()返回的临时对象,被绑定到const引用,只是延长了生命期,access返回后还是会自动析构的。
noowanda 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 blijiang 的帖子:]
如下代码:
const Thing* last_accessed_thing;
void access(const Thing& t) {
// ...
last_accessed_thing = &t;
}

extern Thing get_a_thing();
void f() {
access(get_a_thing());
}
请问为何last_accessed_thing会变成空悬指针?为何get_a_thing会产生临时对象并被access()摧毁?
按照我的理解,access参数是个引用参数,直接取传入参数的地址,并不会产生临时对象。这样t的地址就是get_a_thing()的…
[/Quote]
1. 请问为何last_accessed_thing会变成空悬指针?
const Thing* last_accessed_thing; 因為你把last_accessed_thing所指的內容宣告成const了,初始值沒設的話就是為0.
2. access参数是个引用参数,直接取传入参数的地址,并不会产生临时对象?
access是call by value, 所以他是先產生临时对象,再取地址.
speeder53 2009-02-11
  • 打赏
  • 举报
回复
mk
luxiaoxun 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 deerwin1986 的回复:]
extern Thing get_a_thing();
此函数没有返回左值 怎么能取地址呢。。。
[/Quote]

应该是返回引用吧
deerwin1986 2009-02-11
  • 打赏
  • 举报
回复
extern Thing get_a_thing();
此函数没有返回左值 怎么能取地址呢。。。
tangshuiling 2009-02-11
  • 打赏
  • 举报
回复

产生零时对象并被绑定到常量对象引用上了
waizqfor 2009-02-11
  • 打赏
  • 举报
回复
请问为何last_accessed_thing会变成空悬指针?为何get_a_thing会产生临时对象并被access()摧毁?

get_a_thing 声明就没有产生临时对象啊!~
yndfcd 2009-02-11
  • 打赏
  • 举报
回复
mouse_xie 2009-02-11
  • 打赏
  • 举报
回复
创建一个指针变量吧。。。
taodm 2009-02-11
  • 打赏
  • 举报
回复
extern Thing get_a_thing();
这样申明的函数不产生临时变量,那还有什么是临时变量呢。

65,211

社区成员

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

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