how to find where the memory is freed ?

This is a discussion on how to find where the memory is freed ? within the C++ Programming forums, part of the General Programming Boards category; Hello.. i learn math and C++ and i thouth to combine both to make my life easeir: i wrote a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    how to find where the memory is freed ?

    Hello..

    i learn math and C++ and i thouth to combine both to make my life easeir:
    i wrote a simple class that by using it i will be able to calculate relation matrix in the power n.

    my problem is that in unable to store the object (it getting freed) and i can't store it.
    the function bool_mutilpication need to return a object and store it inside a new one.
    matrix_c = matrix_a * matrix_b

    im able to do (object * object).Print();
    but not object = other_object * more_other_object ; //dobule free.

    Code:
    /***************************************************************************
     *            matrics.cc
     *
     *  Fri Jun  1 09:23:55 2007
     *    Jabka Atu
     *  Email mashrom{d0t}head@gmail.com
     ****************************************************************************/
    
    /*
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     */
     
    #include <iostream>
    #include <assert.h>
    
    using namespace std;
    
    bool** multiply(const bool **matA,int rows,int colmuns,int colmuns_of_sec_matrix, const bool **matB);
    
    class bool_matrix{
    	 int rows;
    	 int colmuns;
    	 
    	 bool** matrix;
    	 
    	 void init();
    	 public:
    		
    	 	bool** _matrix;
    		int size;	
    	 	bool_matrix();
    	 	~bool_matrix();
    	 	bool_matrix(int m,int n);
    	 	bool_matrix(const bool_matrix &other);
    		void Print(); 
    	 	bool_matrix boolean_multiply(const  bool_matrix &other);
    		bool& set_values(int rows,int colmuns);
    	 	bool_matrix operator * (const bool_matrix& other){
    			bool_matrix result(this->boolean_multiply(other));
    			return result;
    		}
    		bool_matrix& operator= (const bool_matrix &other);		
     };
    
    
    bool_matrix& bool_matrix::operator= (const bool_matrix &other){
    	size = other.size;
    	rows = other.rows;
    	colmuns = other.colmuns;
    	matrix = other.matrix;
    	return *this;
    
    }
    void bool_matrix::init(){
    	matrix = new bool* [rows];
    	assert(matrix);
    	for (int i = 0;i < rows ; i++) 	 
    	{
    		matrix[i] = new bool [colmuns];
    		assert(matrix[i]);
    	}
    	/*
    	can be done by .. 
    	for (int i = 0;i  < rows;i++)
    		for (int j=0;j < colmuns;j++)
    			matrix[i][j]=false;
       */
    	
    	for (int i=0 ; i < rows * (colmuns - 1); i++)
    		*(*matrix + i) = false;
    	
    	_matrix = matrix;
    	 
    }
     bool_matrix::bool_matrix() {
    	 rows = 0;
    	 colmuns = 0;
    	 size = 0;
    	 init();
    	 
     }
     bool_matrix::~bool_matrix(){
    	 if (!matrix) //check if there is actually allocated memory
    	 {
    		 for (int i = 0;i < rows;i++)
    			 delete [] matrix[i];
    	 }
    	 delete [] matrix;
    	 
     }
    
     bool_matrix::bool_matrix(int m,int n){
    	 rows    = m;
    	 colmuns = n;
    	 size    = m * n;
    	 init();
    	
    }
    
    bool_matrix::bool_matrix(const bool_matrix &other){
    	this -> rows    = other.rows;
    	this -> colmuns = other.colmuns;
    	this -> size 	= other.size;
    	
    	init();
    	for (int i = 0;i < rows;i++)
    		for (int j = 0;j < colmuns;j++)
    				this->matrix[i][j] = other.matrix[i][j]; 
    	
    }
    
    void bool_matrix::Print(){
    
    	for (int i=0;i<rows;i++)
    	{
    		for(int j = 0; j< colmuns;j++)
    			cout<<matrix[i][j];
    		cout<<endl;
    	}
    	
    }
    
    bool_matrix  bool_matrix::boolean_multiply(const  bool_matrix &other){
    	
    	bool_matrix result((*this).rows ,other.colmuns);
    	bool **returned_matrix = NULL;
    	returned_matrix =  multiply((const bool**) (*this).matrix,(*this).rows,(*this).colmuns,other.colmuns,(const bool**) other.matrix);
    	result.matrix = returned_matrix;
    	return result;
    }
    
    bool& bool_matrix::set_values(int rows,int colmuns){
    	return matrix[rows][colmuns];
    }
    
    bool** multiply(const bool **matA,int rows,int colmuns,int colmuns_of_sec_matrix, const bool **matB){
    	
    	
    	bool **matrix;
    	//create a new matrix for result
    	matrix = new bool* [rows];
    	assert(matrix);
    	
    	for (int i = 0;i < rows; i++)
    		matrix[i] = new bool [colmuns_of_sec_matrix];
    	assert(matrix);
    	//End of creation of matrix.
    	
    	//intlize matrix
    	for (int i = 0;i < rows ; i++)
    		for (int j = 0 ; j < colmuns_of_sec_matrix ; j++)
    			matrix[i][j]= false;
    	//End initlization
    		
    	for (int i=0;i < rows ; i++)
    	{
    		for (int j=0 ;j < colmuns ; j++)
    			for (int k=0 ; k<colmuns_of_sec_matrix ; k++)
    			{
    				if ((matA[i][j] == matB[j][k]) and (matA[i][j] == 1))
    				{
    				
    					matrix[i][k] = true;
    					continue; //if even one pair (a)ij = (b)jk = 1 then (a)ik will be 1.so no need to go to check for more
    				}
    			
    			}
    		
    	}
    		return matrix;
    }
    
    int main(){
        
    	bool_matrix a(4,3),b(3,4),c;
    	
    	a._matrix[0][0]=1;
    	a._matrix[0][1]=1;
    	a._matrix[0][2]=0;
    	a._matrix[1][0]=0;
    	a._matrix[1][1]=1;
    	a._matrix[1][2]=0;
    	a._matrix[2][0]=1;
    	a._matrix[2][1]=1;
    	a._matrix[2][2]=0;
    	a._matrix[3][0]=0;
    	a._matrix[3][1]=0;
    	a._matrix[3][2]=1;
    	
    	b._matrix[0][0]=1;
    	b._matrix[0][1]=0;
    	b._matrix[0][2]=0;
    	b._matrix[0][3]=0;
    	b._matrix[1][0]=0;
    	b._matrix[1][1]=1;
    	b._matrix[1][2]=1;
    	b._matrix[1][3]=0;
    	b._matrix[2][0]=1;
    	b._matrix[2][1]=0;
    	b._matrix[2][2]=1;
    	b._matrix[2][3]=1;
    	
    	c = a * b; //dobule freea 
    	
    	return 0;
     }
    Last edited by jabka; 06-02-2007 at 08:05 AM. Reason: spelling
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    and why do you want to free a and b? they are statically created and continue to exists after your operator usage...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> if (!matrix) //check if there is actually allocated memory
    That should be the other way around. If matrix is null then you can skip the loop.

    However, your problem appears to be in the operator=. Instead of allocating new memory and copying over the data, you just copy the matrix pointer from other to this. That is bad, because now both instances point to the same dynamic data. So later, when both instances are deleted, they both try to free that same memory. So just update you operator= to properly copy the values.

    A better choice might be to use vectors for your dynamic arrays and not have to worry about the memory management, unless of course this is a memory management learning exercise.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    about the copy:
    i though since allready i allocated memory for one i could use it;//why waste memory

    it is for math studies" discrete mathematical structures" (sounds terrfing but actualy pretty easy and helpfull).

    we need to find if some relations are transitive symmetric and reflexive and to calculate Matrix^n
    and since it is hard work (try to calculate matrix of power 6 when dealing 10 on 10 matrix).

    since i study cpp i guessed i could kill to birds with one stone ..

    anyway ..

    i changed to this :

    Code:
    bool_matrix& bool_matrix::operator= (const bool_matrix &other){
    	
    	
    	size = other.size;
    	rows = other.rows;
    	colmuns = other.colmuns;
    	
    	//matrix = other.matrix;
    	init();
    	for (int i = 0;i < rows;i++)
    		for (int j = 0;j < colmuns;j++)
    				this->matrix[i][j] = other.matrix[i][j]; 
    	
    	return *this;
    
    }
    also changed the destructer to :

    Code:
    bool_matrix::~bool_matrix(){
    	 if (matrix != NULL) //check if there is actually allocated memory
    	 {
    		 for (int i = 0;i < rows;i++)
    			 delete [] matrix[i];
    	 
    	 delete [] matrix;
    	 }
     }
    and
    in check in bool_multiplication for rows = columns :
    Code:
    bool_matrix  bool_matrix::boolean_multiply(const  bool_matrix &other){
    	
    	bool_matrix result((*this).rows ,other.colmuns);
    	bool **returned_matrix = NULL;
    	if ((this->colmuns) != (other.rows))
    	{
    		cout<<"undefined multiplication colmuns of first matrix isn't equal rows\n";
    	}
    	else
    	{
    			returned_matrix =  multiply((const bool**) (*this).matrix,(*this).rows,(*this).colmuns,other.colmuns,(const bool**) other.matrix);
    			result.matrix = returned_matrix;
    	}
    	return result;
    }

    it's alive ...

    now being a little bit serious;
    why did that object had been deleted if transfers it adders since the original memory was allocated using new did it mean that i have memory leakage ?

    also:
    i need to somehow implement initialising it with strings and creating an array as in the length of each string :
    bool_matrix(char *str);
    bool_matrix(char *str,char *str);
    but how can i do it for unknown size ?

    how can implement the foo objname[]={0,0,0,0,.... };

    p.s.
    time for me to learn how to use vectors..
    Last edited by jabka; 06-02-2007 at 04:57 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    time for me to learn how to use vectors..
    Here ya go: http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4027/

    Also time for you to learn how to spell..
    Here's a translation table for others to interpret your post:

    thouth -> thought (what you wrote rhymes with mouth!)
    structers -> structures
    semtric -> symmetric
    rflexive -> reflexive
    distructer -> destructor
    sireus -> serious
    initlizing -> initialising
    unkown -> unknown
    preety -> pretty

    oh and 'somehow' should have been one word where you used 'some how'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> though since allready i allocated memory for one i could use it;//why waste memory
    As long as the sizes are the same you can do this. But if you assign a matrix of a different size, then you will want to delete the old memory allocate new memory with the new size before copying the data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. shared memory not getting freed
    By Elkvis in forum Linux Programming
    Replies: 19
    Last Post: 02-29-2008, 03:48 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21