# Question about dangling pointer

Discussion in 'C++' started by John, Nov 4, 2004.

1. ### JohnGuest

Hi:

Below is a simple code:

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

a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;

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, Nov 4, 2004

2. ### Sharad KalaGuest

"John" <> wrote in message
> Hi:
>
> Below is a simple code:
>
> {
> public:
> link1(int &b1, double &b2);
> int* a1;
> double* a2;
> };
>
> 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.

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

Sharad Kala, Nov 4, 2004

3. ### Karthik KumarGuest

John wrote:
> Hi:
>
> Below is a simple code:
>
> {
> public:
> link1(int &b1, double &b2);
> int* a1;
> double* a2;
> };
>
> 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.

Purely coincidental.

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

Karthik Kumar, Nov 4, 2004
4. ### Max M.Guest

John wrote:

> 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?

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

Max M., Nov 4, 2004
5. ### Sharad KalaGuest

Disregard my reply. Apologies.

Sharad Kala, Nov 4, 2004
6. ### Lionel BGuest

Max M. wrote:
> John wrote:
>
> > 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?

>
> 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.

Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
(as pointed out by Karthik) fortuitous that you get the same output for
c3 before and after the delete (IIRC c3 is undefined after delete).

<pedantic>

Karthik Kumar wrote:
> Yup. The moment you say c3->x , it invokes UB . You are
> trying to access a memory location that has been deallocated
> / freed.

Actually the OP doesn't say c3->x after the delete.
</pedantic>

--
Lionel B

--
Lionel B

Lionel B, Nov 4, 2004
7. ### Sharad KalaGuest

"Karthik Kumar" <> wrote in message

> Yup. The moment you say c3->x , it invokes UB . You are
> trying to access a memory location that has been deallocated / freed.

He isn't. Read his code carefully, I made a mistake too.

Sharad Kala, Nov 4, 2004
8. ### Max M.Guest

Lionel B wrote:
>
> Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
> (as pointed out by Karthik) fortuitous that you get the same output for
> c3 before and after the delete (IIRC c3 is undefined after delete).

c3 never gets dereferenced after the delete occurs. Are you arguing that
printing invalid pointer values causes undefined behaviour?

Max

Max M., Nov 4, 2004
9. ### Ron NatalieGuest

Max M. wrote:
> Lionel B wrote:
>
>>Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
>>(as pointed out by Karthik) fortuitous that you get the same output for
>>c3 before and after the delete (IIRC c3 is undefined after delete).

>
>
> c3 never gets dereferenced after the delete occurs. Are you arguing that
> printing invalid pointer values causes undefined behaviour?
>

Absolutely! Use of a deleted pointer value in anyway is undefined.
Last sentence of 3.7.3 says that using the an invalid pointer value
(such as one passed to delete) is undefined.

Ron Natalie, Nov 4, 2004
10. ### Lionel BGuest

Max M. wrote:
> c3 never gets dereferenced after the delete occurs. Are you arguing
> that printing invalid pointer values causes undefined behaviour?

Lionel B wrote previously, choosing his words with immense care:
<quote> ... it is ... fortuitous that you get the same output for c3
before and after the delete ... </quote>

--
Lionel

Lionel B, Nov 4, 2004
11. ### JohnGuest

Karthik Kumar <> wrote in message news:<4189d259\$1@darkstar>...
> John wrote:
> > Hi:
> >
> > Below is a simple code:
> >
> > class link1
> > {
> > public:
> > link1(int &b1, double &b2);
> > int* a1;
> > double* a2;
> > };
> >
> > 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.

>
> Purely coincidental.

The pointers c3->a1 and c3->a2 point to c1 and c2. At LINE3, c1 and c2
are still valid. If the memory that c3->a1 and c3->a2 point to is
reallocated, what will happen to c1 and c2?

john

John, Nov 4, 2004
12. ### Sharad KalaGuest

> The pointers c3->a1 and c3->a2 point to c1 and c2. At LINE3, c1 and c2
> are still valid. If the memory that c3->a1 and c3->a2 point to is
> reallocated, what will happen to c1 and c2?

Nothing.

Sharad Kala, Nov 5, 2004