Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Memory allocation C++

  1. #1
    Join Date
    Nov 2007
    Location
    Mictlan
    Beans
    84
    Distro
    Xubuntu 8.04 Hardy Heron

    Memory allocation C++

    Hi,

    Im working with a small class in C++. I want to create a matrix class that can store a bunch of numbers on it.

    It should be easy, but im having an issue with the destructor. As soon as the program finishes, the destructor generates an error that I can not understand.

    Please, could someone help me with this

    here is the code that I made:

    Code:
    //Matrix object 
    #include <iostream>
    
    class Matrix {
        protected:
    	int m_iRow, m_iCol;
    	double* m_pdData;
    	void copy(const Matrix&);
    
        public:
    	Matrix(int, int);
    	~Matrix();
    	Matrix(const Matrix&);
    	Matrix& operator=(const Matrix&);
    	int nRow(void);
    	int nCol(void);
    	double get(int, int);
    	void set(int,int,double);
    	void create(void);
    };
    
    Matrix::Matrix(int a, int b) {
        //Initialise the matrix with the row and column
        m_iRow = a;
        m_iCol = b;
        create();
    }
    
    Matrix::~Matrix() {
        //Deletes the space of the data
        delete m_pdData;
    }
    
    void Matrix::create(void) {
        //Creates container with zeros
        m_pdData = new double[m_iRow*m_iCol];
    }
    
    Matrix::Matrix(const Matrix& mat) {
        //Copy constructor 
        //Used when a copy of an object is produced
        this->copy(mat);
    }
        
    Matrix& Matrix::operator=(const Matrix& mat) {
        //Assignment operator function
        //Overloads the equal sign operator to work
        //with Matrix objects
        if (this==&mat) return *this;
        delete [] m_pdData;
        this->copy(mat);
        return *this;
    }
    
    int Matrix::nRow(void) {
        //Returns number of rows
        return m_iRow;
    }
    
    int Matrix::nCol(void) {
        //Returns number of columns
        return m_iRow;
    }
    
    double Matrix::get(int i, int j) {
        //Parenthesis operator function
        //Allows acces to values of matrix via (i,j)
        return m_pdData[m_iCol*(i-1) + (j-1)];
    }
    
    void Matrix::set(int i,int j,double val) {
        //set the value val into the matrix
        if (i < m_iRow && j < m_iCol)
    	m_pdData[m_iCol*(i-1) + (j-1)] = val;
    }
    
    void Matrix::copy(const Matrix& mat) {
        //private copy functions
        //copies values from one matrix to another
        m_iRow = mat.m_iRow;
        m_iCol = mat.m_iCol;
        int iData = m_iRow * m_iCol;
        create();
        for (int i=0; i<iData; i++)
    	m_pdData[i] = mat.m_pdData[i];
    }
    
    int main() {
        int n=3;
        Matrix A(n,n);
        A.set(1,0,9.0);
    
        for (int i=0; i<n; i++) 
    	for (int j=0; j<n; j++) {
    	    std::cout << i << "," << j << ":" << A.get(i,j) << "\n";
    	}
    
        return 0;
    }
    -----------------------------------
    Happy owner of a Packard Pell EasyNote A Laptop with UBUNTU without microphone.

  2. #2
    Join Date
    Feb 2008
    Beans
    367

    Re: Memory allocation C++

    I just glanced over it quickly and I did not see anything obvious. It may help if you post the error as well.

    EDIT: I'm not sure of what your goal is. Wouldn't it be easier to dynamically allocate a two-dimensional array?

  3. #3
    Join Date
    Nov 2007
    Location
    São Carlos, Brazil
    Beans
    136
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Memory allocation C++

    Don't hit the Submit button twice, man.

    http://ubuntuforums.org/showthread.p...ghlight=matrix

    Glad you already found your answer, anyways.
    João Paulo Melo de Sampaio
    Computer Engineering Student @ UFSCar
    Site: http://www.jpmelos.com
    Twitter: http://www.twitter.com/jpmelos (@jpmelos)

  4. #4
    Join Date
    Jul 2008
    Location
    Aryadesa
    Beans
    183
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Memory allocation C++

    Program is crashing at Matrix::get() function. Give it a check. (Used gdb)
    Quantum computers are not known to be able to solve NP-complete problems in polynomial time.
    Scott Aaronson's blog

  5. #5
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    480
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Memory allocation C++

    The destructor is using
    Code:
    delete m_pdData;
    instead of
    Code:
    delete [] m_pdData;
    The difference between the array and non-array delete is crucial.

  6. #6
    Join Date
    Oct 2005
    Location
    Queensland, Australia
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Memory allocation C++

    The difference between the array and non-array delete is crucial.
    Why is that? It's only an array of doubles.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Memory allocation C++

    I ran the program using 'ddd'. The error is occurring here:
    Code:
    return m_pdData[m_iCol*(i-1) + (j-1)];
    when the values i = 0 and j = 0 are passed to the function (m_iCol = 3).

    Oh, and as pointed out already, the destructor should be:
    Code:
    delete [] m_pdData;
    Without the [], you will have a memory leak.


    P.S. Don't forget to fix the set() method as well; it has the same bug as the get() method.

  8. #8
    Join Date
    Nov 2007
    Location
    Mictlan
    Beans
    84
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: Memory allocation C++

    Quote Originally Posted by jpmelos View Post
    Don't hit the Submit button twice, man.

    http://ubuntuforums.org/showthread.p...ghlight=matrix

    Glad you already found your answer, anyways.

    Sorry for the double post.
    I didnt notice the error until I saw the duplicated post.


    Thanks for the help I found the error on the code. It was the index that I was using for accessing the array.

    btw, what is ddd?
    is it a debugging tool?
    -----------------------------------
    Happy owner of a Packard Pell EasyNote A Laptop with UBUNTU without microphone.

  9. #9
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Memory allocation C++

    'ddd' is the GUI front-end to 'gdb' (which is a debugger).

  10. #10
    Join Date
    Oct 2005
    Location
    Queensland, Australia
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Memory allocation C++

    Without the [], you will have a memory leak.
    Why is that? It's only an array of doubles. I don't get it!
    Cheers, Mike

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •