i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object "result" through a pointer (and not call by value)..however i aint getting d values of the result in main()...instead i am getting garbage values when i print it...

here is my code :

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

// general function declarations

void list (int *choice);


// class declaration
[B]class matrix
{
	private :
		int rows;
		int columns;
		int * element;

	public :
		matrix();
		matrix (int, int);
		void setdata (int, int, int *);
		void showptrdata();
		matrix* operator+(matrix &);

	friend istream& operator >> (istream &, matrix &);
	friend ostream& operator << (ostream &, matrix &);
	// function overloading -- for printing a pointer object
        friend ostream& operator << (ostream &, matrix *&);

};[/B]


matrix :: matrix ()
{
	rows = 0;
	columns = 0;
	*element = 0;
}

matrix :: matrix (int r ,int c)
{
	rows = r;
	columns = c;
	element = new int [rows * columns];
}

istream& operator >> (istream &read, matrix &mat)
{
	for (int i = 0; i < mat.rows * mat.columns; ++i)
		read >> *(mat.element+i);
	return read;
}

[B]ostream& operator << (ostream &print, matrix &mat)
{
	for (int i = 0; i < mat.rows; ++i)
	{
		for (int j = 0; j < mat.columns; ++j)
		{
			print << *mat.element << "  ";
			++mat.element;
		}
		print << endl;
	}
	mat.element -= mat.rows * mat.columns;
	return print;
}

ostream& operator << (ostream &print, matrix *&mat)
{

	for (int i = 0; i < mat->rows; ++i)
	{
		for (int j = 0; j < mat->columns; ++j)
		{
			print << *mat->element << "  ";
			++mat->element;
		}
		print << endl;
	}
	mat->element -= mat->rows * mat->columns;
	return print;
}
[/B]

[B]matrix*  matrix :: operator + (matrix &matrix2)
{
	matrix result (rows, columns);
	for (int i=0; i< rows * columns; ++i)
		*(result.element+i) = *(element+i) + *(matrix2.element+i);
	//printf ("%d %d\n", result.rows, result.columns);
	//printf ("%u %u %u \n", &result, &result.rows, &result.columns);
	return &result;
}

[/B]
void main()
{
	clrscr();

	int r, c;
	int choice;

	cout << "Enter the rows of the matrix : ";
	cin >> r;
	cout << "Enter the columns of the matrix : ";
	cin >> c;

	matrix matrix1 (r, c);

	cout << "Enter the elements rowwise : " << endl;
	cin >> matrix1;


	cout << "You have created the following matrix : " << endl;
	cout << matrix1;
	cin.ignore();
	cin.get();

	do
	{

		list (&choice);

		switch (choice)
		{
			case 1 :
				cout << endl << endl << "The matrix is as follows : " << endl;
				cout << matrix1;
			break;

			case 2 :
				cout << "Enter the new values of the matrix : " << endl;
				cin >> matrix1;
				cout << "The modified matrix is : " << endl;
				cout << matrix1;

			break;

			case 3 :
[B]				matrix matrix2(r,c);
				cout << "Enter the elements of the second matrix : " << endl;
				cin >> matrix2;
				matrix *result;
				result = matrix1 + matrix2;
				//printf ("%u\n", result);
				//printf ("%u %u\n", &result->rows, &result->columns);
				//printf ("%d %d\n", result->rows, result->columns);
				cout << "The resultant matrix is : " << endl;
			       cout << result;[/B]
			break;

			case 4 :
			break;

			case 5 :
			break;

			case 6 :
			break;

			case 0 :
			exit(0);
		}
		cin.ignore();
		cin.get();
	} while (choice != 0);

}

void list (int *choice)
{
	clrscr();
	cout << "1. Display the matrix." << endl << "2. Modify the matrix." << endl
		  << "3. Matrix Addition." << endl << "4. Matrix subtraction." << endl
		  << "5. Matrix multiplication." << endl
		  << "6. Transpose of matrix." << endl << "0.Exit." << endl;

	cin >> *choice;
	cin.ignore();
	if (!(*choice >= 0 && *choice <= 6))
	{
		cout << "Invalid choice." << endl;
		cin.get();
		list (choice);
	}
}

i have given the bold effect to the code that i felt is related to my problem..so..pls help me with this problem..
and yes..one more question..is there any way to return this "result" by a reference ??? i know it may seem absurd...bcoz of the scope of the local variable...still i would like to learn a way if any... :)

>after the addition i want to return the local object "result" through a pointer
That won't work. Local variables are destroyed when execution leaves their containing scope. What you should be doing is returning a copy of the local variable.

>That won't work. Local variables are destroyed when execution leaves their containing scope. What you should be doing is returning a copy of the local variable.

heyyy...yaaa..i understood my stupidity....that is a local variable i am returning.. :P
actually the pointers are useful when a variable passed from the main() to a function is returned to the main().... :)
anyways...thanks for that...
:)

hey..but then what about the allocation of memory that i have done??
i have actually allocated memory to the "result" using new operator..while declaring it using copy constructor :

matrix result (rows, columns);

then should not the variable retain its value??
or due to the local scope...new has no effect ??

>then should not the variable retain its value??
It should not. The pointer that points to the memory is destroyed along with the object, and you no longer have valid references to the memory you allocated.

To be more specific, here are the levels of memory that you're using:

  • Level 1: The "result" variable, an instance of the matrix class
  • Level 2: The "element" pointer in your matrix class
  • Level 3: The anonymous memory that "element" points to

At the top level you have your matrix object. This is the variable whose lifetime is controlled by the scope of your addition member function. However, when this variable is destroyed, so are its data members. When "result" is destroyed, "element" is destroyed, which is expected and normal. C++ handles its own memory correctly.

However, "element" pointed to memory which you allocated with new, and memory allocated with new can only be released with delete. Unfortunately, because "element" has been destroyed, you can't expected it to continue pointing to the memory you allocated with new. You lost your only reference to the memory, which is a memory leak. Further, "element" is now a dangling pointer because "result" has been destroyed.

heyyy yaaa!! i got it!!
that means i do have that element ...
but i have lost the pointer.... :)
ohkk!!
so it means that the only way is to return a copy of the variable... :)
but if there is any other efficient way to return "result" then suggestions are most welcome... :)

This article has been dead for over six months. Start a new discussion instead.