dynamic 2D array Deletion error

Started by
5 comments, last by mindrot 17 years, 6 months ago
im making a class to generate and store a dynamically size maze im not even worried about anything besides the dynamic allocation and deletion atm here is my code:

struct coord
{
    int x, y;
};
class maze
{
    int mazeWidth;
    int mazeHeight;
    coord start;
    coord end;
    cell** mazeArray;
    public:
        maze(int, int);
        ~maze();
};


maze::maze(int mazeWidthVal, int mazeHeightVal)
{
    mazeWidth = mazeWidthVal;
    mazeHeight = mazeHeightVal;
    cell** mazeArray=NULL;
    mazeArray = new cell*[mazeWidth];
    for(int i=0; i<mazeWidth; i++)
        mazeArray=new cell[mazeHeight];
}
maze::~maze()
{
    for(int i=0; i<mazeWidth; i++)
        delete [] mazeArray;
    delete [] mazeArray;
}

when i comment out the contents of my deconstructor all is well ... except for the whole memory leak possibilities... uncomment i get an error... the error isnt very descriptive but ill post the dump if requested. thanks in advance
Advertisement
man... ponder a problem for an hour
post to ask for help
then solve it myself im a doof

problem with my decontructor was i was deleting the array from the front to the back
instead you need to delete from the back to the front

this is because the array is stored in the form of a bunch of pointers
and when you delete the first pointer it cant find the next
so essentially the memory leak wasnt crashing the prog - it was the index
out of bounds error traversing the array that was

...i think

leaving problem up to help in case anyone else makes this mistake


to fix:
replaced deconstructor with

maze::~maze(){    for(int i=mazeWidth; i>0; i--)        delete [] mazeArray;    delete [] mazeArray;}
Try taking out this line:
cell** mazeArray=NULL;
mazeArray is a member variable, but here you're overriding it with a local variable of the same name, hence your allocation is lost when the function exits. mazeArray itself is never assigned any value, so when you delete it and its sub-arrays the behavior is undefined. Also, IMO this sort of error is a good reason to use some sort of prefix or identifier (such as m_) to differentiate between local and member variables.

This may not be exactly what you want to hear, but overall you'd be better off using std::vector or boost::multiarray, as it will help prevent these sorts of errors, and many, many others.
thanks,
your right, i dont need that line
it was just left over from a non object oriented version of the program
from when i declared the maze globally...
I don't think your follow-up post is the correct fix. In fact, it's now even worse (doesn't delete the first entry in the array, while incorrectly deleting one past the last entry).

I'd go back to your original code and remove the extra line I mentioned.

Also, a bug 'disappearing' in response to some change or another is not always an indication that the change is correct. Remember, it's called 'undefined behavior' for a reason :) Who knows, in your case maybe the entry after the last entry in your array is usually null, so deleting it does no harm. Furthermore, maybe it was deleting the first entry that was crashing the program, and now that your loop is incorrect and doesn't delete it, the bug 'appears' to be gone. Yup, C and C++ are both fairly evil languages, but there are a lot of things you can do (such as using the C++ standard library) that will ease the pain a bit.
im going to type this out to further understand my problem

i make a dynamic array to contain several dynamic arrays

so i have
p[0]->p[1]->p[2]
where each p points to another list which looks conceptually identicle

so i start at p[2] and delete the array it contains
then go to p[1] and delete the array it contains
then i stop at p[0] and simply delete the pointer and not the array it contains?

im pretty rough on this stuff and im starting to wish i used stl vectors...

i went back to the original code and kept the line u suggested i removed out
and it works

which is wierd because the only reason i put that line in there is because it errored on me without...

well it works now with the orignal code - the =NULL; line

/sigh

the only reason i came up with the 2nd solution was because
what i have now w/out the =NULL; line still errored

lol oh well it works now...

my initial explaination on why 2nd solution doesnt work
because the pointers in my first array dont ever get delted
until the end...only the arrays that they point to...

..i think

i dont presume to know anything
which is why im posting to begin with

if my explainations are off please dont hate ^_^

This topic is closed to new replies.

Advertisement