In the snippet below, if the problem line is present, my program
crashes during the Matrix operator*(const Matrix&) call. Would
someone point out to me why I'm having this problem?
Regards,
Ryan
#include <iostream>
#include <cmath>
#include <vector>
#include <exception>
#include <ctime>
using std::cout;
using std::cerr;
using std::endl;
class Matrix
{
private:
int m, n;
double *buffer;
public:
// Constructors
Matrix(int m_, int n_) : m(m_), n(n_), buffer(new double[m*n]){}
~Matrix()
{
if( buffer != NULL )
{
//delete []buffer; //<<<--- This is the problem line
}
}
Matrix& operator=(const Matrix&);
Matrix operator*(const Matrix&) const;
void fill_random();
};
Matrix& Matrix::operator=(const Matrix& A)
{
n = A.n;
m = A.m;
if( buffer != NULL ){
delete [] buffer;
buffer = NULL;
}
buffer = new double[m*n];
for(int i=0; i<n*m; i++)
buffer[i] = A.buffer[i];
return *this;
}
Matrix Matrix::operator*(const Matrix& A) const
{
Matrix C(m, A.n);
if(n == A.m)
for( int i=0; i<m; i++ )
for( int j=0; j<A.n; j++ )
for( int k=0; k<n; k++ )
C.buffer[i+j*C.m] += buffer[i+k*m]*buffer[k+j*C.m];
else
throw std::length_error("length_error exception in
Matrix::operator*(Matrix)");
return C;
}
void Matrix::fill_random()
{
long seed;
time(&seed);
srand(seed);
for( int i=0; i<m*n; ++i)
buffer[i] = double(rand()) / RAND_MAX;
}
int main()
{
int n = 3;
Matrix A(n,n);
Matrix B(n,n);
Matrix C(n,n);
try{
A.fill_random();
B.fill_random();
C = A * B;
}
catch( exception& e ){
std::cout << e.what() << std::endl;
}
catch(...){
std::cout << "Unknown exception" << std::endl;
}
return 0;
} 2 3302
"Tino" <ti****@yahoo.com> wrote... In the snippet below, if the problem line is present, my program crashes during the Matrix operator*(const Matrix&) call. Would someone point out to me why I'm having this problem?
First of all let me note that you've not followed the "Rule of Three"
in your code. More details below. Regards, Ryan
#include <iostream> #include <cmath> #include <vector> #include <exception> #include <ctime>
using std::cout; using std::cerr; using std::endl;
class Matrix { private: int m, n; double *buffer;
public: // Constructors Matrix(int m_, int n_) : m(m_), n(n_), buffer(new double[m*n]){}
You simply _must_ define the copy c-tor if you have dynamic memory
used in your class. Read more about "Rule of three".
Matrix(const Matrix& m_) : m(m_.m), n(m_.n),
buffer(new double[m*n])
{
std::copy(m_.buffer, m_.buffer + m*n, buffer);
}
~Matrix() { if( buffer != NULL ) { //delete []buffer; //<<<--- This is the problem line
You're probably deleting it twice. The added copy-c-tor should
take care of that. Restore the delete:
delete[] buffer;
} } Matrix& operator=(const Matrix&); Matrix operator*(const Matrix&) const; void fill_random(); };
Matrix& Matrix::operator=(const Matrix& A) { n = A.n; m = A.m;
if( buffer != NULL ){ delete [] buffer; buffer = NULL; }
buffer = new double[m*n]; for(int i=0; i<n*m; i++) buffer[i] = A.buffer[i];
return *this; }
Matrix Matrix::operator*(const Matrix& A) const { Matrix C(m, A.n); if(n == A.m) for( int i=0; i<m; i++ ) for( int j=0; j<A.n; j++ ) for( int k=0; k<n; k++ ) C.buffer[i+j*C.m] += buffer[i+k*m]*buffer[k+j*C.m]; else throw std::length_error("length_error exception in Matrix::operator*(Matrix)"); return C; }
void Matrix::fill_random() { long seed; time(&seed); srand(seed); for( int i=0; i<m*n; ++i) buffer[i] = double(rand()) / RAND_MAX; }
int main() { int n = 3;
Matrix A(n,n); Matrix B(n,n); Matrix C(n,n);
try{ A.fill_random(); B.fill_random(); C = A * B; } catch( exception& e ){ std::cout << e.what() << std::endl; } catch(...){ std::cout << "Unknown exception" << std::endl; } return 0; }
HTH
Victor
"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om... In the snippet below, if the problem line is present, my program crashes during the Matrix operator*(const Matrix&) call. Would someone point out to me why I'm having this problem?
I haven't checked everything in detail, but your class
lacks a copy-constructor:
Matrix( Matrix const& orig );
(implementation shall be similar to the assignment op,
except that there are no previous contents to destroy).
~Matrix() { if( buffer != NULL ) { //delete []buffer; //<<<--- This is the problem line } }
NB: the test for NULL above is redundant:
delete[] NULL; is required to be a no-op.
Matrix& Matrix::operator=(const Matrix& A) { n = A.n; m = A.m;
if( buffer != NULL ){ delete [] buffer; buffer = NULL; }
NB: this assignment operator is unsafe in case of
self-assignment, and also isn't exception-safe.
The best way to solve both problems is to
allocate the new buffer first, copy the
contents, then release the old buffer.
hth,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, XiTact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Douglas Peterson |
last post by:
Take a look at this code, it looks funny as its written to be as short as
possible:
-- code --
struct Base
{
~Base() { *((char*)0) = 0; }
};
struct Derived : public Base
|
by: Timothy Madden |
last post by:
Hello
I have a linked list of object of a class. I thought it would be nice to
have the destructor delete the whole list when I delete just the first
element. I don't want to recursivly destroy...
|
by: Timothy Madden |
last post by:
Hy
I have destructors that do some functional work in the program flow.
The problem is destructors should only be used for clean-up, because
exceptions might rise at any time, and destructors...
|
by: Gutek |
last post by:
Hello all,
I have run into the following problem with virtual destructor. I have
following classes:
class A
{
public:
double* p;
A(unsigned int s=0) : { p = new double ; }
|
by: frs |
last post by:
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class....
|
by: Ken Durden |
last post by:
I am in search of a comprehensive methodology of using these two
object cleanup approaches to get rid of a number of bugs,
unpleasantries, and cleanup-ordering issues we currently have in our...
|
by: Peter Oliphant |
last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have
a class derived from Form that creates an instance of one of my custom
classes via gcnew and stores the pointer in a...
|
by: Ben Voigt |
last post by:
I have a POD type with a private destructor. There are a whole hierarchy of
derived POD types, all meant to be freed using a public member function
Destroy in the base class. I get warning C4624....
|
by: gw7rib |
last post by:
I've been bitten twice now by the same bug, and so I thought I would
draw it to people's attention to try to save others the problems I've
had. The bug arises when you copy code from a destructor...
|
by: itdevries |
last post by:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |