关于悬挂指针的问题 [英] Question about dangling pointer

查看:179
本文介绍了关于悬挂指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


下面是一个简单的代码:


class link1

{

public:

link1();

link1(int& b1,double& b2);

int * a1 ;

double * a2;

};


link1 :: link1(int& b1,double& b2) {

a1 =& b1;

a2 =& b2;

}


int main(){

int c1 = 10;

double c2 = 0.5;

link1 * c3 = new link1(c1,c2) ;


int * p1;

double * p2;

p1 = c3-> a1;

p2 = c3-> a2;


std :: cout<<" c3:"<< c3<<"" P1:"<< * P1<<" p2:"<< * p2<< std :: endl; // LINE1


删除c3; // LINE2


std :: cout<<" c3:"<<< c3<<" P1:"<< * P1<<" P2:"<< * P2<<的std :: ENDL; // LINE3


返回0;

}


在LINE2,c3指向的内存被释放,所以c3变成了一个悬挂指针的b / b
。 c3-> a1和c3-> a2怎么样?他们晃来晃去了吗?

指针?

c3指向的内存现在可以重新分配了吗?


我运行了代码。 LINE1和LINE2的输出是相同的。


非常感谢。


john

Hi:

Below is a simple code:

class link1
{
public:
link1();
link1(int &b1, double &b2);
int* a1;
double* a2;
};

link1::link1(int &b1, double &b2){
a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;
link1* c3 = new link1(c1, c2);

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?
Can the memory that c3 pointed to be reallocated now?

I ran the code. The output of LINE1 and LINE2 are the same.

Thanks a lot.

john

推荐答案



" John" <乔********* @ yahoo.com>在消息中写道

"John" <jo*********@yahoo.com> wrote in message
嗨:

下面是一个简单的代码:

class link1
{
public:
link1();
link1(int& b1,double& b2);
int * a1;
double * a2;
};

link1 :: link1(int& b1,double& b2){
a1 =& b1;
a2 =& b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;
link1 * c3 = new link1(c1,c2);

int * p1;
double * p2;
p1 = c3-> a1;
p2 = c3-> a2;

std :: cout<< " C3:"<< C3<<" P1:"<< * P1<<" p2:"<< * p2<< std :: endl; // LINE1

删除c3; // LINE2

std :: cout<<"" c3:"<<< c3<<"" P1:"<< * P1<<" P2:"<< * P2<<的std :: ENDL; // LINE3

返回0;


在LINE2,c3指向的内存被释放,所以c3变成了悬空指针。 c3-> a1和c3-> a2怎么样?他们叼着指针吗?


删除后尝试访问内存是未定义的行为。

行为。

c3指向的内存是否可以现在重新分配?


是的,系统已经回收了内存并且可以随意使用它。

我运行了代码。 LINE1和LINE2的输出相同。
Hi:

Below is a simple code:

class link1
{
public:
link1();
link1(int &b1, double &b2);
int* a1;
double* a2;
};

link1::link1(int &b1, double &b2){
a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;
link1* c3 = new link1(c1, c2);

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?
Trying to access memory after its deletion is a source of undefined
behavior.
Can the memory that c3 pointed to be reallocated now?
Yes, system has reclaimed the memory and can use it at its will.
I ran the code. The output of LINE1 and LINE2 are the same.




你的意思是LINE1和LINE3。这就是未定义的行为的全部意义,它好像b $ b似乎有效,但可以随时打破。


Sharad



You mean LINE1 and LINE3. That''s what undefined behavior is all about, it
seems to work but can break any time.

Sharad


John写道:
嗨:

下面是一个简单的代码:

class link1
{
public:
link1();
link1(int& b1,double& b2);
int * a1;
double * a2;
link1 :: link1(int& b1,double& b2){
a1 =& b1;
a2 =& b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;
link1 * c3 = new link1(c1,c2);

int * p1;
double * p2;
p1 = c3-> a1;
p2 = c3-> a2;

的std :: COUT<<" C3:"<< C3<<" P1:"<< * P1<<" p2:"<< * p2<< std :: endl; // LINE1

删除c3; // LINE2

std :: cout<<"" c3:"<<< c3<<"" P1:"<< * P1<<" P2:"<< * P2<<的std :: ENDL; // LINE3

返回0;


在LINE2,c3指向的内存被释放,所以c3变成了悬空指针。
c3-> a1和c3-> a2怎么样?他们叼着指针吗?


是的。你说c3-> x的那一刻,就会调用UB。你是试图访问已经解除分配/释放的内存位置的



c3指向的内存现在可以重新分配吗?


当然 - 是的。
我运行了代码。 LINE1和LINE2的输出是相同的。
Hi:

Below is a simple code:

class link1
{
public:
link1();
link1(int &b1, double &b2);
int* a1;
double* a2;
};

link1::link1(int &b1, double &b2){
a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;
link1* c3 = new link1(c1, c2);

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer.
How about c3->a1 and c3->a2? Are they dangling
pointers?
Yup. The moment you say c3->x , it invokes UB . You are
trying to access a memory location that has been deallocated / freed.
Can the memory that c3 pointed to be reallocated now?
Of course - yes.
I ran the code. The output of LINE1 and LINE2 are the same.




纯属巧合。


-

Karthik。 http://akktech.blogspot.com

''从我的电子邮件中删除_nospamplz给我发邮件。 ''



Purely coincidental.

--
Karthik. http://akktech.blogspot.com .
'' Remove _nospamplz from my email to mail me. ''


John写道:
John wrote:
在LINE2,c3指向的内存被释放,所以c3变为 a2怎么样?他们是悬空指针吗?
At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?




删除c3后,c3-> a1和c3-> a2不再存在。这并不意味着他们的价值变得无效。


在删除''c3''之前,你保存了c3->的价值。 a1和c3-> a2分别为p1和

p2,现在指向c1和c2(因为它们是通过

引用传递给link1'的ctor) 。因此,p1和p2是有效的指针,并且

你的程序就像人们期望的那样。


Max



After deleting c3, c3->a1 and c3->a2 no longer exist. That''s doesn''t imply
their value turns invalid.

Before deleting ''c3'' you saved the value of c3->a1 and c3->a2 into p1 and
p2 respectively, which now point to c1 and c2 (as they were passed by
reference to link1''s ctor). Therefore, p1 and p2 are valid pointers and
your program behaves as one would expect.

Max


这篇关于关于悬挂指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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