Dangling Pointers

Started by
8 comments, last by kilah 13 years, 3 months ago
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
Advertisement

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.
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.
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.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


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 );
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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

A deep copy doesn't help in this situation: the problem is due to a pointer assignment, not an object assignment.
Ah yes, I've re-read the post and see it is a completely different problem.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

[size=2]<blockquote>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. <br /> [size=2]<br /> <br /> [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: &quot;Never trust a programer who thinks a pointers is smarter than he is&quot;. <br /> [size=2]<br /> <br /> [size=2]P.S: That doesn&#39;t mean you don&#39;t use complex pointers, but it means you have to know when to use them, and why to do so.

This topic is closed to new replies.

Advertisement