Hello,
I have a project that needs to implement a Safe Array which is an Array that has bounds checking. On top of that I need to create a "Matrix" class which implements 2D which is basically a Safe array within a Safe array. This is also templatized.
The problem is it compiles fine with my eclipse but when I try to compile
in Unix it gives me this error: *** glibc detected *** double free or corruption: 0x0937d008 ***
Can someone check my code to see if there is anything wrong with it?
Also: Does anyone know why when it calls my copy constructor when making the 2D Safe Array? I have to comment it out inorder for it to work.
Here is my code:
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
template <class T>
class SA{
private:
int low, high;
T* p;
public:
// default constructor
SA(){
low = 0;
high = -1;
p = NULL;
}//constructor
// 2 parameter constructor SA x(10,20);
SA(int l, int h){
if( (h - l + 1) <= 0 ){
cout << "constructor error in bounds definition" << endl;
exit(1);
}//if
low = l;
high = h;
p = new T[h-l+1];
}//2 parameter constructor
// single parameter constructor
// SA x(10); and getting an array x indexed from 0 to 9
SA(int i){
low = 0;
high = i - 1;
p = new T[i];
}//singple parameter constructor
/*
// copy constructor for pass by value and initialization
SA(const SA & s){
int size = s.high - s.low + 1;
p = new T[size];
for(int i=0; i<size; i++)
p[i] = s.p[i];
low = s.low;
high = s.high;
}//copy constructor
*/
// destructor
~SA(){
delete [] p;
}//destructor
//overloaded [] lets us write
//SA x(10,20); x[15]= 100;
T& operator[](int i){
if(i < low || i > high){
cout << "index "<< i << " out of range" << endl;
exit(1);
}//if
return p[i-low];
}//overloaded [] operator
// overloaded assignment lets us assign one SA to another
SA & operator=(const SA s){
if(this == &s)
return *this;
delete [] p;
int size = s.high - s.low + 1;
p = new T[size];
for(int i = 0; i < size; i++)
p[i] = s.p[i];
low = s.low;
high = s.high;
return *this;
}//overloaded = operator
// overloads << so we can directly print SAs
friend ostream& operator<<(ostream& os, SA s){
int size = s.high - s.low + 1;
for(int i = 0; i < size; i++)
cout << s.p[i] << endl;
return os;
};
};//class SafeArray
//Matrix class
template <class T>
class Matrix{
private:
SA < SA<T> > mat;
public:
//2 parameter constructor
Matrix(int row, int col){
mat = SA< SA<T> >(0, row-1);
for(int i = 0; i < row; i++){
mat[i] = SA<T>(0, col-1);
}//for
}//construct for 2 parameters
//4 parameter constructor
Matrix(int r1, int r2, int c1, int c2){
mat = SA< SA<T> >(r1, r2-1);
for(int i = r1; i < r2; i++)
mat[i] = SA<T>(c1, c2-1);
}//4 parameter constructor
SA<T> operator[](int r){
return(mat[r]);
}//operator[] overload
};//class Matrix
int main(){
Matrix <int>arr(5,10);//create 2D array of 5 rows and 10 cols
Matrix <int>arr2(10,20,100,200);//create 2D array of 10 rows from 10-19 and 100 cols from 100-199
arr[3][8]=1234;//test
cout << arr[3][8] << endl;
arr2[12][155]=1111;//test
cout << arr2[12][155] << endl;
//testing out of bounds
arr[5][6]=1;
int i = arr2[22][200];
return 0;
}//main