Public Group

# Dangling Pointers

This topic is 2895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys,

Quick question about pointers in c++. Recently I was reviewing pointers in C++ and I stumbled on this website <http://www.codeguru.com/forum/showthread.php?t=312742> and the post has a example of a lost pointer:

 class Sample { int* val; public: Sample() { val = new int; *val = 44; } ~Sample() { delete val; } }; void foo() { Sample* a = new Sample; Sample* b = new Sample; a = b; delete a; //actually deletes b //delete b; //already deleted } 

I'm just wondering whats the best way to deal with this situation and how would you go about deleting a?

Thanks

##### Share on other sites

Hey guys,

Quick question about pointers in c++. Recently I was reviewing pointers in C++ and I stumbled on this website <http://www.codeguru....ad.php?t=312742> and the post has a example of a lost pointer:

 class Sample { int* val; public: Sample() { val = new int; *val = 44; } ~Sample() { delete val; } }; void foo() { Sample* a = new Sample; Sample* b = new Sample; a = b; delete a; //actually deletes b //delete b; //already deleted } 

I'm just wondering whats the best way to deal with this situation and how would you go about deleting a?

Thanks

Well if you are doing it entirely manually you'll have to delete a before you assign b. However, programmers tend to use smart pointers to take care of this situation. You should google auto_ptr and boost::shared_ptr for further information.

##### Share on other sites
Yes I do use smart pointers but I have an interview coming up and thought it would be a good idea to brush up on doing things manually.

##### Share on other sites
Some interviewers would shoot you for trying to manage memory manually. Especially if you do not properly encapsulate it (e.g. making Sample noncopyable). "Raw" programming impresses no one, it must be correct first.

As for your question, there is no easy way to handle this other than to move away from raw pointers, or just being careful.

##### Share on other sites
As said you should avoid that by avoiding the use of raw pointers in the first place. But no one's perfect. In which case there are memory leak detectors would help find such problems while you debug your program.

##### Share on other sites

As said you should avoid that by avoiding the use of raw pointers in the first place. But no one's perfect. In which case there are memory leak detectors would help find such problems while you debug your program.

The detector I am using is the std one I'm guessing..? I came across it on the msdn site.

 #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 

##### Share on other sites
You just need to make a copy constructor that preforms a "deep" copy , off the top of my head I think something like this should work:

Sample(const Sample &rhs) { val = new int; *val = *rhs.val; } 

You may have to also do the assignement copy constructor which I can't remember, maybe:
operator=(const Sample&rhs); or some such (been a wh ile since I've overloaded an operator).

Alternately you could just disable copying (by making it private)
private: Sample(const Sample &); // You don't even need to implement it, just define it There is a class in boost, I think its boost:non_copyable. If you inherit from that then it will prevent your object being copied.

##### Share on other sites
A deep copy doesn't help in this situation: the problem is due to a pointer assignment, not an object assignment.

##### Share on other sites
Ah yes, I've re-read the post and see it is a completely different problem.

##### Share on other sites
[color=#1C2837][size=2]
However, programmers tend to use smart pointers to take care of this situation. You should google auto_ptr and boost::shared_ptr for further information. [/quote]
[color=#1C2837][size=2]

[color=#1C2837][size=2]A really strong suggestion from my personal experience: if you are applying to a position where you require a language that requires memory management (e.g: C++), knowing raw pointers is something noone will be impress at, but will assume as basic knowledge. As they say: "Never trust a programer who thinks a pointers is smarter than he is".
[color=#1C2837][size=2]

[color=#1C2837][size=2]P.S: That doesn't mean you don't use complex pointers, but it means you have to know when to use them, and why to do so.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013741
×