Thread: Deallocate an array of pointer

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    14

    Deallocate an array of pointer

    This is how I allocate space for a matrix


    Code:
    //Initialize matrix
    	*matrix= (float**) malloc(Rows * sizeof(float *));
    	for (i=0;i<Rows;i++){
    		(*matrix)[i]=(float *) malloc(MCols * sizeof(float));
    		for (j=0 ;j<MCols;j++){
    			(*matrix)[i][j]=0.0;
    		}
    	}
    how can I deallocate the same matrix?

    I used this code:

    Code:
    void deallocateMatrix(float ***matrix, int numRows){
    	int i;
    	for(i=0;i<numRows;i++){
    		free((*matrix)[i]);
    	}
    	free(*matrix);
    }
    (is it correct??)

    but it doesn't works. And nothing again with **matrix in input.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    It looks correct, at a glance. How did you determine that it does not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    the compiler ends with a message indicating that there is an error on the line with "free((*matrix)[i]);"
    If it is correct... I have to control the dimension in input (numRows)..maybe..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    it would help if you posted the actual compiler error message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deallocate Memory
    By Nuzut in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2011, 10:06 AM
  2. Using free() to deallocate memory
    By Stiletto in forum C Programming
    Replies: 1
    Last Post: 04-24-2011, 07:47 AM
  3. How to deallocate space with free.
    By brack in forum C Programming
    Replies: 18
    Last Post: 08-27-2010, 01:41 AM
  4. deallocate pointer
    By sangit in forum C++ Programming
    Replies: 16
    Last Post: 03-13-2009, 01:08 PM
  5. Deallocate Memory and reuse pointer
    By appleGuy in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2007, 11:07 AM