Question about dangling pointer

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

  1. John

    John Guest

    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, Nov 4, 2004
    #1
    1. Advertising

  2. John

    Sharad Kala Guest

    "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
    Sharad Kala, Nov 4, 2004
    #2
    1. Advertising

  3. 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. '
    Karthik Kumar, Nov 4, 2004
    #3
  4. John

    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
    #4
  5. John

    Sharad Kala Guest

    Disregard my reply. Apologies.

    Sharad
    Sharad Kala, Nov 4, 2004
    #5
  6. John

    Lionel B Guest

    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
    #6
  7. John

    Sharad Kala Guest

    "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
    Sharad Kala, Nov 4, 2004
    #7
  8. John

    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
    #8
  9. John

    Ron Natalie Guest

    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
    #9
  10. John

    Lionel B Guest

    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
    #10
  11. John

    John Guest

    Karthik Kumar <> wrote in message news:<4189d259$1@darkstar>...
    > 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.


    Thanks for your reply.
    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
    #11
  12. John

    Sharad Kala Guest


    > 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
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rootz anabo
    Replies:
    0
    Views:
    460
    rootz anabo
    Feb 3, 2005
  2. Hans Van den Eynden

    dangling reference

    Hans Van den Eynden, Oct 16, 2004, in forum: Java
    Replies:
    1
    Views:
    2,857
    Joona I Palaste
    Oct 16, 2004
  3. __PPS__
    Replies:
    20
    Views:
    868
    __PPS__
    Oct 16, 2005
  4. sg
    Replies:
    6
    Views:
    275
    Juha Nieminen
    Apr 17, 2011
  5. Jarek Blakarz
    Replies:
    2
    Views:
    345
    Jarek Blakarz
    Nov 6, 2012
Loading...

Share This Page