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;
};
Pointers and container classes
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:
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).
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.
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
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.
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.
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.
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.
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement