[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

RE: c++: 2D array



Guh !
	A slightly bloated but robust code follows . Purify says no memory
leaks ( 0 bytes :) and that __is_good__ !
However, not that the inhyerent pointer manipulation in C makes this program
susceptible to access beyond array bounds. Couldn't figure a way to get
across that :( In all, I should think that this is pretty good C++ code :)

	I ahve tried to keep the C++ code as simple as possible. Pleae
suggest modifications to effect robustness, usability.

Regards,
Shourya



/*****
* foo.cxx
* Report bugs to Shourya.Sarcar@xxxxxxxxxxxx
* A templatised 2D matrix class
* Compile on IRIX 6.5 : $ /usr/bin/CC -o foo foo.cxx
****/

#include <iostream.h>

template <class T>
class Matrix{
public:
	T * matrix ;
	
	
private:
	int rows;
	int cols;
	int size;

public :
	Matrix (int r, int c) {
		rows = r;
		cols =c;
		size=rows*cols;
		matrix = new T [size];
		cout << "Rows : " <<rows<<" Cols : " <<cols<< " Size : "
<<size<<endl;
	}
		
	/* This is the tricky part : We overload just [], return a pointer
to T, and let the C++ compiler do the pointer manipulation in usercode 
	So,in the usercode, if a is a Matrix <T> , then  a[3][4] is
interpreted as matrix_3 [4], where you think of matrix_3 as a simple T*
(pointer to type T) and the rest is imple pointer manipulation as in C */



	T* operator[](int x) {
		/* I am adding bounds checking logic due to two reasons :
			a) Code bloat due to Exception handling etc.
			b) The security is partial, we are still depending
on C's insecure pointer handling mechanism to resolve the second subsript
			That is, if some one tries to access a[30][40] in a
30 X 30 array, there is not much we can do :(
		*/
		return (&matrix[(x * cols)]);
	}

	//copy constructor 

	Matrix (const Matrix <T>& from) {
		rows=from.rows;
		cols = from.cols;
		size=rows*cols;
	//	cout << "[Copy ctor ] Rows : " <<rows<<" Cols : " <<cols<< "
Size : " <<size<<endl;
		matrix  = new T [size];
		for (int index = 0; index<size; index++) {
		matrix [index] = from.matrix[index];

		/* note if the obejct being copied does not have a proper
assignment operator overloading, then this assignment could lead to problems
*/

		}
	}

	// assignment operator overloading
	Matrix& operator= (const Matrix <T>&  from) {
		if (from == *this) return (*this) ; //protect against self
assignment	
		rows=from.rows;
		cols =from.cols;
		size=rows*cols;
	//	cout << "[Assignment] Rows : " <<rows<<" Cols : " <<cols<< "
Size : " <<size<<endl;
		matrix  = new T [size];
		for (int index = 0; index<size; index++) {
		matrix [index] = from.matrix[index];

		/* note if the obejct being copied does not have a proper
assignment operator overloading, then this assignment could lead to problems
*/

		}
	}
			
		

	//destructor
	~Matrix() {
	//	cout << "Destroying " <<matrix<<endl;
		delete [] matrix;
	}

};
	
typedef float  myType; // or whatever int , foo , doubble

int main() {

	Matrix <myType> b (5,4);

/* 	An array of (5x4) gives access of array [0-4][0-3] */
	b[3][1] =5; 
	cout <<b[3][1]<<endl;
	Matrix <myType> n = b;
	cout << n[3][1]<<endl;
	
	return 0;
}






#-----Original Message-----
#From: Shridhar Daithankar [mailto:shridhard@xxxxxxxxxx]
#Sent: Thursday, September 21, 2000 11:55 AM
#To: linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx
#Subject: Re: [LIP] c++: 2D array
#
#
#Hi
#
#There is a book from Yashawant Kanetkar, something like Perls 
#in C programming,
#where he has many such tricks explained. Though in C.
#
#. The book is bit outdated I presume. It talks about DOS and 
#all. I haven't
#check the latest edition.
#
#
#And I feel that memcpy feature + dtor can give you nice and 
#robust interface.
#
# Bye
#  Shridhar
#
#"Sarcar, Shourya (MED)" wrote:
#
#> the memcpy feature is cool but beware, if you delete ptr, 
#you have the
#> problem of dangling pointers with ptr[1,2,3,4]
#> but anyway, i like the recursive structure :)
#>
#> shourya
#
#
#---------------------------------------------
#Find out more about this and other Linux India
#mailing lists at http://lists.linux-india.org/
#