Dynamic memory problems[SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
SPR_Phantom
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 21
Joined: Mon Oct 03, 2011 10:58 am

Dynamic memory problems[SOLVED]

Post by SPR_Phantom »

Solved, I needed to do a deep copy of my array!
Started to fool around with dynamic memory again and my testing class worked nice until I started passing it into methods or functions, this is where things got a little unpredictable.
If I have any method that takes my dynamic 2d array class as an argument it works once and then crashes the second time (as if the method some how makes the original null and then tries to use it again after).

Code: Select all

#ifndef dyn2d_H
#define dyn2d_H
template <class mtype>
class dyn2d
{
    private: // unused in test class

    public:
    mtype** element;
    int xlength, ylength;
    dyn2d(int xl, int yl)
    {
        xlength = xl;
        ylength = yl;
        element = new mtype*[xl];
        for (int i = 0; i < xl; i++)
        {
            element[i] = new mtype[yl];
        }
    }
    ~dyn2d()
    {
        for(int i = 0; i < xlength; i++)
        {
            delete [] element[i];
        }
        delete [] element;
    }
};
#endif
It's still a test, so it's not structured properly, but whatever, it's supposed to work either way.

Keep in mind if I do the same operations that I do in the function within the main function ( or wherever the instance was originally from ) it works fine... :S

Perhaps this is something you guys have experienced before?

EDIT: for clarity it's a segment error
Last edited by SPR_Phantom on Fri Oct 14, 2011 5:20 pm, edited 1 time in total.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Dynamic memory problems

Post by qpHalcy0n »

Solved. Shallow copy of array.
Post Reply