473,511 Members | 12,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

any suggestion please

hello C++ gurus,

I am having a problem and want help fom you.
Here is my problem-------------

I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:

class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator

private:
int row;
int column;
int** data;

};

matrix::matrix(int r, int c)
{
//assign row and columns
row = r;
column = c;
//now allocate memory for data
data = new int* [row];
for(int i = 0; i < column; ++i)
data[i] = new int[column];

}

matrix::~matrix()
{
//we need to free the memory allocated earlier
if( data != NULL)
{
for(int i = 0; i < row; ++i)
delete[] data[i];
delete []data;
}
}

matrix matrix::operator + (const matrix& x)
{
matrix temp(row,column);

//add 2 matrices and store the result into temp

return temp;
}

If I want to add 2 matrices as

matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;

then the following sequence of function calls happens:
1. firstly invokes matrix::operator + (matrix& x).
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.

Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
I do not want to solve this problem by using a 2nd parameter of type
matrix and passing "c" in this parameter.
any suggestion in this regards will be most welcome.

thanks

bisuvius

Nov 8 '06 #1
1 1283
bisuvious wrote:
I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:

class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator

private:
int row;
int column;
int** data;

};
[snipped implementation]
>
If I want to add 2 matrices as

matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;

then the following sequence of function calls happens:
1. firstly invokes matrix::operator + (matrix& x).
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
Right. Also the copy constructor matrix::matrix (const matrix&) is
called to copy the result to the temporary that will live outside the
call a + b to be assigned to c. Although you didn't supply such a copy
constructor, there is one, e.g. the one that gets generated by the
compiler. This automatically generated copy constructor will copy the
class bit for bit, which means that you copy a pointer that already got
deleted. Hence the crash.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.

Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
You are fine only out of coincidence. Provide a copy constructor and you
have done this thing properly.

Regards,
Stuart
Nov 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2776
by: RT | last post by:
I have followed the step by step instructions in the login manual Created login page Use LSC01 This works - login and get redirected to the proper page My problem is the cookie variables...
11
2056
by: John Wellesz | last post by:
Hello, It would be great if there was an option to tell PHP to let the user manage all the HTTP headers instead of sending what it thinks is good for the programmer... For example when you...
0
6359
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
3
1671
by: red floyd | last post by:
I have a suggestion for the standard library.... This is sort of a combination of std::transform() and std::for_each(). It applies the binary function to each iterator in ...
3
1381
by: dreams2text | last post by:
Check out the article titled : Dear Mr Gates, A suggestion to make the CLR Ubiquitous at http://dreams2text.blogspot.com/
15
1446
by: Jon Skeet | last post by:
I've been briefly musing on what is probably a pretty silly idea, but one which would no doubt benefit from being discussed and thoroughly shot down in flames rather than being allowed to fester in...
0
1025
by: sklett | last post by:
I don't create windows application very often, in fact I rarely create a complete application, but more often am writing plugins. I have found that when I sit down to create a windows app, I...
2
1192
by: vinay | last post by:
I have a scenario, need your suggestion.. Our clients are already using the forms authentication where we check the User/Pwd from SQL svr Database. We also have some SETTINGS for the user saved...
1
1365
by: Mark | last post by:
Hi, I want to learn multi-threding in C++. After serching in amazon, I still cant decided which one i should buy. Anyone have suggestion of a book on practical threading in C++? Or is that any...
20
2158
by: Allan Ebdrup | last post by:
I have a suggestion for C# I would like reader/writer locks to be built in to the language. When you want to aquire a loct on an object o you write lock(o) { ...//critical region } I would...
0
7367
Oralloy
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,...
0
7430
jinu1996
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...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
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...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.