Dynamic memory problems[SOLVED]
Posted: Fri Oct 14, 2011 3:52 pm
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).
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
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