Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 12.09.2010, 16:09   #1
ignis_divine
 
Регистрация: 11.09.2010
Сообщений: 5
По умолчанию Работа деструктора

При удалении последнего объекта деструктор выбрасывает исключениe:

Unhandled exception at 0x102d31ea (msvcr90d.dll) in Lab_work_1.exe: 0xC0000005: Access violation reading location 0xfdfdfdf1.

А отладчик останавливается в файле dbgdel.cpp В чем может быть проблема? Спасибо.

head.cpp

Код:
#include "Matrix.h"
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Matrix first(2,3);
	Matrix second(3,2);
	Matrix empty(2,2);
	
	first.fill_rand();
	second.fill_rand();
	
	first.display();
	second.display();

	first.multi(second, empty);
	empty.display();
	 
	return 0;
}
matrix.h
Код:
class Matrix
{
private:
	static bool rand_use;
	bool result;					// Result of summation or multiplying.
	int x, y;						// Number of strings and columns.
	long int** mx;					// Matrix.
public:
	Matrix(void);					// Default constructor.
	Matrix(int x, int y);			// Usual constructor.
	Matrix(const Matrix &);			// Copy constructor
	~Matrix(void);					// Destructor.
	void fill_hand();				// Filling the matrix by yourself.
	void display();					// Displaying the content of object.
	void fill_rand();				// Random filling of matrix.
	bool summa(Matrix &, Matrix &); // Summation return correctness of execution.
	bool multi(Matrix &, Matrix &); // Multiplying return correctness of execution.
};
Matrix.cpp
Код:
#include "Matrix.h"
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

Matrix::Matrix(void)
{
	result = true;
}

Matrix::Matrix(int x1, int y1)
{
	result = true;
	x = x1;
	y = y1;
	
	mx = new long int*[x];
	
	for (int i = 0; i < x; i++)
	{
		mx[i] = new long int[y];
		for (int j = 0; j < y; j++)
		{
			mx[i][j] = 0 ;
		}
		cout<<endl;
	}
}

Matrix::Matrix(const Matrix &from)
{
	Matrix to(from.x, from.y);
	
	to.x = from.x;
	to.y = from.y;
	to.result = from.result;

	for (int i = 0; i < to.x; i++)
	{
		for (int j = 0; j < to.y; j++)
		{
			to.mx[i][j] = from.mx[i][j];
		}
	}
}

Matrix::~Matrix(void)
{
	for (int i = 0; i < y; i++)
	{
		delete[] mx[i];
	}
	delete [] mx;

}

void Matrix::fill_rand()
{
	if (!rand_use) 
	{
		srand(time(0));
		rand_use = true;
	}
	for (int i = 0; i < x; i++)
	{
		for (int j = 0; j < y; j++)
		{
			mx[i][j] = rand()%50;
		}
	}
}

void Matrix::fill_hand()
{
	system("cls");
	cout<<"Please enter the values of matrix[";
	cout<<x<<"]"<<"["<<y<<"] in integer format.";
	for (int i = 0; i < x; i++)
	{
		for (int j = 0; j < y; j++)
		{
			cout<<"\n"<<i<<" "<<j<<" element is: ";
			cin>>mx[i][j];
		}
	}	
}

void Matrix::display()
{
	system("cls");
	cout<<"Matrix ["<<x<<"]["<<y<<"]:";
	for (int i = 0; i < x; i++)
	{
		cout<<endl;
		for (int j = 0; j < y; j++)
		{
			cout<<" "<<mx[i][j];
		}
	}
	cout<<endl;
	system("pause");
}

bool Matrix::summa(Matrix& summand, Matrix& blank)
{
	
	if (( x == summand.x) && (y == summand.y )) 
	{
		for ( int i = 0; i < x; i++ )
			for ( int j = 0; j < y; j++ )
			{
				blank.mx[i][j] = mx[i][j] + summand.mx[i][j];
	 		}
		return true;
	}
	else
	{
		return false;
	}
	
}

bool Matrix::multi(Matrix& ier, Matrix& blank)
{
	if (y == ier.x)
	{
		for ( int i = 0; i < x; i++ )
			for ( int j = 0; j < ier.y; j++ )
				for ( int k = 0; k < ier.x; k++ )
					blank.mx[i][j] = blank.mx[i][j] + mx[i][k]*ier.mx[k][j];  
					
		return true;
	}
	else
	{
		return false;
	}
}

bool Matrix::rand_use = false;
ignis_divine вне форума Ответить с цитированием
Старый 12.09.2010, 17:08   #2
p51x
Старожил
 
Регистрация: 15.02.2010
Сообщений: 15,830
По умолчанию

Код:
for (int i = 0; i < x; i++)
	{
		mx[i] = new long int[y];
Код:
	for (int i = 0; i < y; i++)
	{
		delete[] mx[i];
Создаете х массивов, уничтожаете у...
p51x вне форума Ответить с цитированием
Старый 12.09.2010, 17:43   #3
ignis_divine
 
Регистрация: 11.09.2010
Сообщений: 5
По умолчанию

Спасибо, работает. А не могли бы вы пояснить, как правильно создавать/удалять динамический массив (матрица) на основе указателя?

Последний раз редактировалось ignis_divine; 12.09.2010 в 17:47.
ignis_divine вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Ругается на минус при создание деструктора Hichigo Общие вопросы C/C++ 10 21.08.2010 07:33
Использование конструкторов и деструктора при проектировании пользовательского класса НеважНо Общие вопросы C/C++ 1 22.01.2010 14:56
Задача. Работа с псевдослучайными последовательностями (ПСП). Работа с цветом. 0101 Помощь студентам 3 17.12.2009 23:57
Использование виртуального деструктора Fataller Общие вопросы C/C++ 3 12.12.2009 22:08