472,780 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Question about dangling pointer

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
Jul 22 '05 #1
11 1875

"John" <jo*********@yahoo.com> 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
Jul 22 '05 #2
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. '
Jul 22 '05 #3
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

Jul 22 '05 #4

Disregard my reply. Apologies.

Sharad
Jul 22 '05 #5
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

Jul 22 '05 #6

"Karthik Kumar" <ka*******************@yahoo.com> 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


Jul 22 '05 #7
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

Jul 22 '05 #8
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.
Jul 22 '05 #9
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

Jul 22 '05 #10
Karthik Kumar <ka*******************@yahoo.com> 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
Jul 22 '05 #11

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.

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Aravind | last post by:
I would like to know in what manner dangling pointers affect the security of a application developed using C++.What are the loopholes that are created by dangling pointers and how they could be...
10
by: John | last post by:
Hi: I have a question from the book---C++ primer, page 677. char *arena = new char; Image *ptr = new (arena) Image( "Quasimodo" ); After the above two lines, arena and ptr point to the same...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
11
by: Xiaoshen Li | last post by:
Dear Sir, I am a little puzzled about a function returning a class object, for example, suppose I hava a class Money and a method: Money lastYear(Money aMoney) { Money tempMoney; ......
1
by: sekhar_ps | last post by:
if we store some value at the place in memory which void pointer references then we increment void pointer this leads to dangling pointer?can any one explain whats the reasons for dangling pointer
5
by: madhusagar79 | last post by:
I got one issue to find the Dangling Pointer. Is there any way to find the Dangling Pointer?
3
by: shivapadma | last post by:
1.when referenced pointer is not active then it is called dangling pointer. is this correct ? 2.the pointer which does not point to anything is called null pointer. is NULL macro is a...
1
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) ...
8
by: =?Utf-8?B?TWFyaw==?= | last post by:
Here are three things that I thought would be equivalent but are not. Just wondering why: XmlElement foo = doc.CreateElement("foo"); // load it up with a body xslt.Transform (foo, null, new...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.