Hello everyone,

This program compiles fine and runs fine in Eclipse(C++ IDE) but when I compile it in UNIX and Visual Studio its fine but when I run it, it gives me an error.

For Unix is segmentation fault

The problem is when I initialize VNT, it calls Matrix and after it goes through the Matrix constructor, it calls the destructor for some reason.

Can someone please tell me what is wrong with the code. Thanks for your time.

#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <limits.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;
    };
	
	operator T*(){return p-low;}//operator * overload

};//class SafeArray


//Matrix class
template <class T>
class Matrix{
private:
	SA < SA<T> > mat;
	
public:
	//default constructor
	Matrix(){
		
		
	}//default constructor
	
	//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
	
	~Matrix(){
		delete mat;		
	}//destructor
	
	SA<T>& operator[](int r){
		return(mat[r]);
	}//operator[] overload
	
	operator SA<T>*() {return mat;}//operator * overload
	
};//class Matrix

class VNT{
private:
	Matrix <int> table;//create a int based matrix
	int r;
	int c;
	
public:
	//2 parameter constructor
	VNT(int row, int col){
		table=Matrix<int>(row,col);
		r = row;
		c = col;
	}//VNT 2 parameter constructor
	
	~VNT(){
		delete table;
	}//destructor
	
	SA<int>& operator[](int r){
		return(table[r]);
	}//operator[] overload
	
	void init(){
		//initialize A to empty by making all values INT_MAX
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				table[i][j] = INT_MAX;
			}//for
		}//for
	}//init
	
	void display(){
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				cout << table[i][j] << " ";
			}//for
			cout << endl;
		}//for
	}//display
	
	
	
};


int main(){

	VNT A(5,7);
	A.init();
	A.display();
	
return 0;

}//main

Neither Matrix nor VNT need an explicit destructor, but the real problem is in the destructor for those two classes you try to delete objects that weren't returned by new:

> delete mat;
mat isn't a pointer, it's an object of type SA<SA<T> > . The SA class handles its own memory and doesn't need to be deleted.

> delete table;
table isn't a pointer either, it's an object of type Matrix<int> . The Matrix class uses the SA class, so it doesn't need to be deleted either.

Only use delete as the symmetric counterpart to new.

commented: Thanks so much! +1

Wow, Thanks, I cant believe I missed that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.