any suggestion please

Discussion in 'C++' started by bisuvious, Nov 8, 2006.

  1. bisuvious

    bisuvious Guest

    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 = 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;
    delete []data;
    }
    }

    matrix matrix::eek:perator + (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::eek:perator + (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
     
    bisuvious, Nov 8, 2006
    #1
    1. Advertising

  2. 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::eek:perator + (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
     
    Stuart Redmann, Nov 8, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuaWVs?=

    Any Suggestion ?

    =?Utf-8?B?RGFuaWVs?=, Jan 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    384
    pjabbott
    Jan 26, 2004
  2. B
    Replies:
    4
    Views:
    464
    sloan
    Jan 25, 2006
  3. John
    Replies:
    13
    Views:
    614
    Andrew Thompson
    May 6, 2004
  4. KK
    Replies:
    2
    Views:
    706
    Big Brian
    Oct 14, 2003
  5. mahesh
    Replies:
    3
    Views:
    314
    Jim Langston
    Jan 23, 2007
Loading...

Share This Page