Sign in to follow this  
Kustaz

Pointers and container classes

Recommended Posts

Kustaz    178
Hi, I'm trying to learn more about pointers and container classes. i decided to try building a container that wraps around a pointer, and can add extra features (garbage collection and such). I've been having a few troubles. Here is the code for the container:

template <class T> class Pointer
{	
public:

	typedef unsigned short size_t;
	typedef unsigned short index_t;

	Pointer() {tData = 0;};
	Pointer(T *tN) {tData = tN;};

	~Pointer() {delete[] tData;};

	void operator = (const T tN) {*tData = tN;};

	T * operator -> () {return *(&tData);};
	T * operator [] (index_t ID) {return &tData[ID];};
	T * operator * () {return *tData;};
	T & operator & () {return &tData;};

	operator  T() {return *tData;};



private:

	T *tData;	

};


It works fine, until I try something like this: Pointer<char> TEST = new char; TEST = "dfsdff"; which compiles, but causes a debug assertion failure. Any help is much appreciated, which also includes any feedback about my code (please be constructive though).

Share this post


Link to post
Share on other sites
Fruny    1658
Your destructor is using delete[] on something that hasn't been allocated with new[], namely the "dfsdff" string literal.

Note that your code also suffers from a memory leak when you assign the string literal, and that using delete[] on something that was allocated with new char; is also wrong.

char* ptr = new char; → delete ptr;
char* ptr = new char[10]; → delete[] ptr;
const char* ptr = "hello"; → do nothing

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Evil Steve
I don't see how that compiles - Your operator=() takes a char, not a char*.


It compiles because he is dereferencing tData and then assigning tN to it. Now whether he intended on doing that is another story.

Share this post


Link to post
Share on other sites
Kustaz    178
Hey, what would be the best way to decide which version of delete to use? I now understand how to use it properly, I just dont know the cleanest way to make the code select the correct way.

Share this post


Link to post
Share on other sites
Kustaz    178
Hey, what would be the best way to decide which version of delete to use? I now understand how to use it properly, I just dont know the cleanest way to make the code select the correct way.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Kustaz
Hey, what would be the best way to decide which version of delete to use? I now understand how to use it properly, I just dont know the cleanest way to make the code select the correct way.


Given a pointer, you have absolutely no way of automatically figuring out the right way (if any) to dispose of it. You have to explicitely tell the system what to do. This is why Boost's smart pointer classes exist in both flavours: shared_ptr and shared_array, not to mention the fact that you can specify your own destruction function.

Share this post


Link to post
Share on other sites
Kustaz    178
Oh, good point. I think I still have a lot to learn about how much is too much :). I'v managed to fix everything, but for future reference, is there a tool I can use that will help me detect memory leaks? And also, is there a way to detect wether someone is trying to assign an array when they shouldnt be?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this