琢磨不透的指针丢失,不可思议

yangbc 2005-06-16 12:20:14
//不可思议,下面指针j怎么在地址无效的情况下又指到函数内部去了,什么原因???

#include "stdafx.h"
#include <IOSTREAM>
using namespace std;


void f1( int *& j)
{
int l=20;
int *k=&l;
j=k;
k=0;
}

void any_function_use_local_variables0()
{
int v,x;
cout<<"f0=v="<<v<<endl;
v=110;
x=102;
cout<<"f0=v="<<v<<endl;
}

void any_function_use_local_variables()
{
int a,b,c;
cout<<"f=a="<<a<<endl;
a=b=c=120;
cout<<"f=b="<<b<<endl;
}
int main()
{
int i=10;
int *j=&i;
f1(j);
cout<<*j<<endl; //20
int i4;
int i2=99;

any_function_use_local_variables();
cout<<"1 *j="<<*j<<endl; //120
cout<<"1 j="<<j<<endl; //120
any_function_use_local_variables0();
int i3=98;
cout<<"2 *j="<<*j<<endl; //120
cout<<"2 j="<<j<<endl; //120
return 0;
}
...全文
261 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangbc 2005-06-16
  • 打赏
  • 举报
回复
失效
yangbc 2005-06-16
  • 打赏
  • 举报
回复
只是一个警告而已,我是想知道指针实效后为什么又跑到其它不相干函数内部去了
fhvk 2005-06-16
  • 打赏
  • 举报
回复
void f1( int *& j)
{
int l=20;
int *k=&l;
j=k;
k=0;
}

//这里面有问题。
yangbc 2005-06-16
  • 打赏
  • 举报
回复
zhouhuahai(大草原) , siyang(思扬) 的回答应该比较合理,可以信服
idll 2005-06-16
  • 打赏
  • 举报
回复
j成了野指针
mostideal 2005-06-16
  • 打赏
  • 举报
回复
mark!!
zhangqiushuang 2005-06-16
  • 打赏
  • 举报
回复
看不懂,指针和引用是我最迷糊的地方。再加上这么多的弯弯绕绕,晕了。在工作中没有人这么写吧
siyang 2005-06-16
  • 打赏
  • 举报
回复
//不可思议,下面指针j怎么在地址无效的情况下又指到函数内部去了,什么原因???
j地址无效是什么意思?j是一个整形指针的应用,在调用f1之前它是i的地址的别名;在f1中它被绑定到变量l的地址上。虽然在f1运行完之后变量l销毁,但是j还是那个地址的引用。在any_function_use_local_variables和any_function_use_local_variables0中分配的第二个变量的地址都是j所引用的那个地址,所以出现上面的结果。(看输出结果中带星号的三行)
#include <IOSTREAM>
using namespace std;


void f1( int *& j)
{
int l=20;
int *k=&l;
j=k;
cout<<"***********originality address of j is:"<<j<<"***********"<<endl;
k=0;
}

void any_function_use_local_variables0()
{
int v,x;
cout<<"f0=v="<<v<<endl;
v=110;
x=102;
cout<<"address of v:"<<&v<<endl;
cout<<"***********address of x:"<<&x<<"***********"<<endl;
cout<<"f0=v="<<v<<endl;
}

void any_function_use_local_variables()
{
int a,b,c;
cout<<"f=a="<<a<<endl;
a=b=c=120;
cout<<"address of a:"<<&a<<endl;
cout<<"***********address of b:"<<&b<<"***********"<<endl;
cout<<"address of c:"<<&c<<endl;
cout<<"f=b="<<b<<endl;
}
int main()
{
int i=10;
int *j=&i;
f1(j);
cout<<*j<<endl; //20
int i4;
int i2=99;

any_function_use_local_variables();
cout<<"1 *j="<<*j<<endl; //120
cout<<"1 j="<<j<<endl; //120
any_function_use_local_variables0();
int i3=98;
cout<<"2 *j="<<*j<<endl; //120
cout<<"2 j="<<j<<endl; //120
return 0;
}

zhouhuahai 2005-06-16
  • 打赏
  • 举报
回复
回复人: yangbc(土豆块)
只是一个警告而已,我是想知道指针实效后为什么又跑到其它不相干函数内部去了

之所以这样是因为
f1函数内部的int l是一个局部变量,它在return后就自动销毁了,而j=&l, 也就是说j还指向那块地址。
那块内存经系统回收后,其它函数使用了那块地址。


sunman1982 2005-06-16
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4070/4070047.xml?temp=.9817774
chinaamber 2005-06-16
  • 打赏
  • 举报
回复
是不是因为在f1里面这个j=k; 中的j成了k的地址了。所以最后指针j也是等于0了?我感觉好像是这样。不过我学的不是很好。还有就是定义int *& j的地方,我也不是很理解。
llmsn 2005-06-16
  • 打赏
  • 举报
回复
int *& j这样定义,有必要吗?

64,648

社区成员

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

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