Question about dangling pointer

J

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
 
S

Sharad Kala

John said:
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
 
K

Karthik Kumar

John said:
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.
 
M

Max M.

John said:
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
 
L

Lionel B

Max said:
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 said:
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>
 
S

Sharad Kala

Karthik Kumar said:
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
 
M

Max M.

Lionel said:
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
 
R

Ron Natalie

Max said:
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.
 
L

Lionel B

Max said:
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>
 
J

John

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


Of course - yes.

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
 
S

Sharad Kala

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top