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