472,805 Members | 1,641 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,805 software developers and data experts.

What is a dangling pointer, and how to avoid it?

what is dangling pointer, how it ishappen in program, what isthe solution
Aug 31 '07 #1
3 8216
dmjpro
2,476 2GB
what is dangling pointer, how it ishappen in program, what isthe solution
Right now I am working on J2EE means Java.
I totally Detached C or C++ or VC++ what I worked for 2 or 3 years.
But now a days I have look after those if I get time except my Project Works.
As far as I know, Dangling of pointer....When a pointer refers an Object which is already got destroyed.
And what is cause..It may be from our wrong Coding or some others.
I am not sure about the others.
And if the problems arised from you then the solution is in your Hand.
You need to take care of Object Construction and Destruction.

Kind regards,
Dmjpro.
Aug 31 '07 #2
Meetee
931 Expert Mod 512MB
what is dangling pointer, how it ishappen in program, what isthe solution
Dangling pointer is a pointer that points to an object that is already deleted. The following code illustrates this situation:

Expand|Select|Wrap|Line Numbers
  1. MyClass* p(new MyClass);
  2. MyClass* q = p;
  3. delete p;
  4. p->DoSomething();   //  p is now dangling!
  5. p = NULL;           // p is no longer dangling
  6. q->DoSomething();   // q is still dangling!
A popular technique to avoid dangling pointers is to use smart pointers. Please search google to know more about smart pointers.

Regards
Aug 31 '07 #3
JosAH
11,448 Expert 8TB
A popular technique to avoid dangling pointers is to use smart pointers. Please search google to know more about smart pointers.
There's also a ver nice article in the C/C++ Articles section about "handles"
which can do exactly what the OP wants.

kind regards,

Jos
Aug 31 '07 #4

Sign in to post your reply or Sign up for a free account.

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...
11
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
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...
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...
2
by: ravi50 | last post by:
what is dangling pointer
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( ) ...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
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...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
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.