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{
	int low, high;
	T* p;
	// default constructor 
		low = 0; 
		high = -1;
		p = NULL;
	// 2 parameter constructor SA x(10,20); 
	SA(int l, int h){
		if( (h - l + 1) <= 0 ){
		cout << "constructor error in bounds definition" << endl;
		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
		delete [] p;

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

class VNT{
	Matrix <int> table;//create a int based matrix
	int r;
	int c;
	//2 parameter constructor
	VNT(int row, int col){
		r = row;
		c = col;
	}//VNT 2 parameter constructor
		delete table;
	SA<int>& operator[](int 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;
	void display(){
		for(int i=0; i<r; i++){
			for(int j=0; j<c; j++){
				cout << table[i][j] << " ";
			cout << endl;

int main(){

	VNT A(5,7);
return 0;

10 Years
Discussion Span
Last Post by ff4930

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.

Votes + Comments
Thanks so much!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.