Thread: How do I fix free allocation?

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

    How do I fix free allocation?

    Code:
        int i, j;
        C = malloc(ROWS * sizeof(float **));
        for (i = 0; i < ROWS; ++i)
        {
            C[i] = malloc(ROWS * sizeof(float *));
            for (j = 0; j < ROWS; ++j)
            {
                C[i][j] = malloc(6);
                
            }
        }
    Code:
    void free_data(float ***data)
    {
    
    
        int i, j;
        for (i = 0; i < COLS; i++) {
            for (j = 0; j < ROWS; j++) {
                free((data)[j][i]);
            }
            free((*data)[i]);
        }
        free(data);
    }
    when im trying to run i got error
    can you help me please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that when you allocate, you're only looping over ROWS, both in the outer and inner loops.

    My guess is that you want the outer loop to loop over ROWS and the inner loop to loop over COLS (i.e., row-major). If so, change both your allocation and deallocation code to loop in the same way.

    Note that this means that if you allocate for C[i], you will deallocate for data[i], not (*data)[i].

    Also, malloc(6) is probably wrong: you likely want malloc(float) instead. Unless your want to model a case where a float does not exist (hence represented by a null pointer), this would be an unnecessarily expensive approach: it would be cheaper just to allocate COLS number of floats per row. Later, after getting this to work, you could try your hand at an even cheaper approach: one allocation for the entire 2D array of floats as a single large flat dynamic array, and another for an array of pointers so that the array of floats can be used as a 2D array.

    Besides these, you should check that malloc did not return a null pointer before using what it returns.
    Last edited by laserlight; 05-30-2020 at 03:34 AM.
    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
    Registered User
    Join Date
    Apr 2020
    Posts
    4
    is there a " like " button to put when people give feedback? i love jow everyone takes the time to help each other.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This appears to have been superseded by Problem of allocation memory, so I shall close this thread.
    Last edited by laserlight; 06-01-2020 at 05:54 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. license-free lock-free data structure library updated
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 12-30-2015, 05:50 PM
  2. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  3. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  4. does C automaticlly free mem allocation?
    By msshapira in forum C Programming
    Replies: 2
    Last Post: 01-26-2009, 03:37 AM
  5. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM

Tags for this Thread