heap error
Jan 29, 2011 at 10:41am UTC
i get this error at the beginining of run the project
"Windows has triggered a breakpoint in diplomatiki_me_arrays.exe.
This may be due to a corruption of the heap, and indicates a bug in diplomatiki_me_arrays.exe or any of the DLLs it has loaded."
i use the following class and when in delete(~Array2d) i change lengthrow with sizerow it runs but stops after many many calculations(it is a simulation)
any idea??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
template <typename T>
struct Array2D
{
private : int lengthrow ,lengthcolumn;// -> max_rows
int multiply;
public : T **Data ;
int sizerow,sizecolumn; // -> rows
Array2D() // contructors
{
lengthrow = 10;
lengthcolumn = 10;
multiply = 2;
Data = new T*[lengthrow];
for (int i=0;i<lengthrow;i++)
{
Data[i] = new T[lengthcolumn];
}
sizerow = 0;
sizecolumn =0;
}
Array2D(int lengrow,int lengcol) // contructors
{
lengthrow = lengrow;
lengthcolumn= lengcol;
multiply = 2;
Data = new T*[lengthrow];
for (int i=0;i<lengthrow;i++)
{
Data[i] = new T[lengthcolumn];
}
sizerow = 0;
sizecolumn=0;
}
/* T* operator[](int index)
{
return Data[index];
}
*/
Array2D (const Array2D &a) //copy constructor.
{
lengthrow = a.lengthrow;
lengthcolumn = a.lengthcolumn;
multiply = a.multiply;
sizerow = a.sizerow;
sizecolumn = a.sizecolumn;
Data = new T *[lengthrow];
for (int i=0;i<lengthrow;i++)
{
Data[i] = new T[lengthcolumn];
}
for (int i=0;i<a.sizerow;i++)
for (int j=0; j < a.sizecolumn ; j++)
Data[i][j]=a.Data[i][j];
}
~Array2D()
{
if (Data)
{
for (int i=0;i<lengthrow;i++) delete [] Data[i];
delete [] Data;
Data = NULL;
}
sizerow =0;
sizecolumn=0;
}
Array2D<T>& operator = (Array2D <T> &a)
{
if (this == &a) return *this ;
if (a.sizerow > lengthrow)
{
lengthrow = a.lengthrow;
lengthcolumn = a.lengthcolumn;
multiply = a.multiply;
for (int i=0;i<lengthrow;i++) delete [] Data[i];
delete [] Data;
Data = new T*[lengthrow];
for (int i=0;i<lengthrow;i++)
{
Data[i] = new T[lengthcolumn];
}
}
sizerow = a.sizerow;
sizecolumn = a.sizecolumn;
for (int i=0;i<a.sizerow;i++)
for (int j=0;j<a.sizecolumn;j++)
{
Data[i][j]=a.Data[i][j];
}
//copy(a.Data, a.Data + a.lengthrow , Data);
return *this ;
}
};
Last edited on Jan 29, 2011 at 11:36am UTC
Jan 29, 2011 at 4:26pm UTC
In your assignment operator, you change lengthrow before you loop through your array to delete the existing elements.
Jan 29, 2011 at 10:25pm UTC
this is correct what you say but it doesnt make difference..
it must be another problem that causes this error
Jan 30, 2011 at 2:17am UTC
Why is Data public? Any part of the program can corrupt it without detection. The whole point of information hiding is to prevent these difficult to solve problems.
Make Data private, provide get/set methods with range checking.
Topic archived. No new replies allowed.