Thread: cant free my memory

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    18

    cant free my memory

    hello ,i need some help with free memory
    my allocation:
    (pixel is struct of RGB)
    Code:
    pixel** array(int Y, int X) {
        pixel** theArray;
        theArray = (pixel**)malloc(Y * sizeof(pixel*));
        for (int i = 0; i < Y; i++) {
            theArray[i] = (pixel*)malloc(X * sizeof(pixel));
        }
        return theArray;
    Code:
    void freeMatrix(pixel ** pmatrix, size_t rows){            for (int i = 0; i < rows; ++i)
                {
                    free((pmatrix)[i]);\\problem is here
                }
            free(pmatrix);
            pmatrix = NULL;
        }
    what am i doing wrong ?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    pmatrix needs to be a triple pointer to be able to set it to NULL in the caller.
    Code:
    void freeMatrix(pixel *** pmatrix, size_t rows){
        for (int i = 0; i < rows; ++i)
            free((*pmatrix)[i]);
        free(*pmatrix);
        *pmatrix = NULL;
    }
    However, a better way to allocate a 2-d array is like this:
    Code:
    pixel** matrix_new(int rows, int cols) {
        pixel** m = malloc(rows * sizeof *m);   // allocate row pointers
        m[0] = calloc(rows * cols, sizeof **m); // allocate data block
        for (int i = 1; i < rows; ++i)          // set row pointers
            m[i] = m[i - 1] + cols;
        return m;
    }
     
    void matrix_delete(pixel*** m) {
        free((*m)[0]);  // delete data block
        free(*m);       // delete row pointers
        *m = NULL;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    18
    Quote Originally Posted by john.c View Post
    pmatrix needs to be a triple pointer to be able to set it to NULL in the caller.
    Code:
    void freeMatrix(pixel *** pmatrix, size_t rows){
        for (int i = 0; i < rows; ++i)
            free((*pmatrix)[i]);
        free(*pmatrix);
        *pmatrix = NULL;
    }
    However, a better way to allocate a 2-d array is like this:
    Code:
    pixel** matrix_new(int rows, int cols) {
        pixel** m = malloc(rows * sizeof *m);   // allocate row pointers
        m[0] = calloc(rows * cols, sizeof **m); // allocate data block
        for (int i = 1; i < rows; ++i)          // set row pointers
            m[i] = m[i - 1] + cols;
        return m;
    }
     
    void matrix_delete(pixel*** m) {
        free((*m)[0]);  // delete data block
        free(*m);       // delete row pointers
        *m = NULL;
    }
    i tried to free with the first function and its stiil doesnt work.
    you know what is the problem?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    Quote Originally Posted by cbeginner12334 View Post
    its stiil doesnt work.
    okay
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. How does free() know how much memory it has to free?
    By shiroaisu in forum C Programming
    Replies: 14
    Last Post: 09-09-2011, 11:28 AM
  3. How to Free All Memory ?
    By sergioms in forum C Programming
    Replies: 52
    Last Post: 01-15-2009, 05:02 PM
  4. free RAM memory
    By koyboy in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2008, 07:03 AM
  5. When to free up memory?
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 09-27-2005, 03:22 PM

Tags for this Thread