memory freeing function

This is a discussion on memory freeing function within the C Programming forums, part of the General Programming Boards category; Hi! I'm supposed to do some matrix handling functions for a school project. But i'm having trouble with function that ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    memory freeing function

    Hi!

    I'm supposed to do some matrix handling functions for a school project. But i'm having trouble with function that frees memory allocated for the matrix. I use
    Code:
      retMatrix=(double **)malloc(rows*sizeof(double *));
      for(i=0;i<rows;i++)
       {
        retMatrix[i]=(double *)malloc(columns*sizeof(double));
       }
    to allocate the memory. And

    Code:
    void matrixDestruct(double **matrix)
    {
       int i=0;
        while(matrix[i]!=NULL)
        {
         free(matrix[i]);
         i++;
        }  
       free(matrix);
    }
    to free it but the destructor gives a memory handling error when i run it my program with walgrind. I know that the problem is matrix[i]!=NULL but i cant think of any other way to determine what is the last row of the matrix. Use of global variables is not allowed. Anyone got ideas?
    Last edited by johndoe; 02-16-2006 at 11:39 PM.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by johndoe
    Hi!

    Code:
    void matrixDestruct(double **matrix)
    {
       int i=0;
        while(matrix[i]!=NULL)
        {
         free(matrix[i]);
        }  
       free(matrix);
    }
    You are not updating i above, so you are trying to free matrix[0] forever.

    Also, please see the casting malloc FAQ.

    Also, I don't see any code in your original post that contains an explicit NULL at the end of your matrix array.
    Last edited by cwr; 02-16-2006 at 10:59 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Yea, sorry i++; got leftout cause i didnt copy&paste it. But the problem is that matrix[i] is some freaky value even there is memory allocated and when theres no memory allocated to it situation doesnt change its still some huge number. So the problem is finding out what is the last row in the 2-dimensional double array.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't just expect it to magically stop. You have to keep track of the size of your matrix. Pass it as a function argument. Either that, or allocate an extra pointer, for the "row", set it to NULL, and stop when you reach it. Of course if you forget to set it to NULL, you're hosed.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > for(i=0;i<rows;i++)
    Just use the same for loop you used to allocate the memory with, to free it again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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