Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about dangling pointer

Reply
Thread Tools

Question about dangling pointer

 
 
John
Guest
Posts: n/a
 
      11-04-2004
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
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      11-04-2004

"John" <> wrote in message
> 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.


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

Sharad


 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      11-04-2004
John wrote:
> 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.


Purely coincidental.

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
 
Reply With Quote
 
Max M.
Guest
Posts: n/a
 
      11-04-2004
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



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      11-04-2004

Disregard my reply. Apologies.

Sharad


 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      11-04-2004
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

 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      11-04-2004

"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




 
Reply With Quote
 
Max M.
Guest
Posts: n/a
 
      11-04-2004
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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-04-2004
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.
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      11-04-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cyclic references, shared_ptr, dangling pointer Jarek Blakarz C++ 2 11-06-2012 10:44 AM
Dangling Pointer issue sg C++ 6 04-17-2011 07:45 AM
Dangling pointer quiz question __PPS__ C++ 20 10-16-2005 03:22 AM
DesignRules:331 Dangling RAMB16A output: (Help) rootz anabo VHDL 0 02-03-2005 04:04 PM
dangling reference Hans Van den Eynden Java 1 10-16-2004 09:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57