473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 8271
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
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.