Thread: This code results in a memory leak, right?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    This code results in a memory leak, right?

    Code:
    void free_2D(double **array)
    {
        free(array[0]);
        free(array);
    }
    Assuming that we have already allocated a 2D array of doubles with both dimensions > 1, using this to free it will result in a memory leak, yes?

    Because I was sure that you needed to loop over the second dimensions and free each array[i] separately before freeing array, but this is part of a code that seems to work. So am i wrong about how things are freed, or is it working only because it coincidentally doesnt run out of memory?

    it should be (I think?)

    Code:
    void free_2D(double **array, int xdim)
    {
        int i = 0;
        for (i = 0; i < xdim; i++)
        {
            free(array[i]);
        }
        free(array)
    }
    Last edited by KBriggs; 05-06-2010 at 08:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like a possible memory leak, but what is the code that allocates the memory? You need that to be certain.
    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
    Jun 2009
    Posts
    486
    Code:
    void* calloc_2D(int N0, int N1)
    {
        int i;
        double **array;
    
        if( ( array = (double **) calloc( N0, sizeof(double *)) ) == NULL ){
    	fprintf(stderr,"error in calloc_2D() \n");
    	return NULL;
        }
    	
        if( ( array[0] = (double *) calloc( N1 * N0, sizeof(double))) == NULL ){
    	fprintf(stderr,"error in calloc_2D() \n");
    	return NULL;
        }
    
        for( i=0; i<N0; ++i){
    	
    	array[i] = array[0] + i*N1; // pointer arithmetic
        }
    
        return array;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Observe that there are only two calls to calloc(). Therefore, the two calls to free() make sense, and in fact are correct.

    You are probably thinking of a case where for each pointer to double, a calloc() is used, but here the idea is to use calloc() once to allocate enough memory for the entire 2D array, and then use a loop to get the pointers to double to point to the correct double in this 1D array, and thereby allow one to access the 1D array as a 2D array.
    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

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Huh, never seen it done that way before. Are there advantages to using a 1D array as opposed to a 2D array?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KBriggs
    Are there advantages to using a 1D array as opposed to a 2D array?
    In a way, you are using a 2D array, and in a way, you aren't. The same goes for a solution that uses malloc()/realloc()/calloc() for each pointer to double.

    As for the advantage: obviously, you have fewer calls to functions that allocate memory.
    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

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Cool, I wouldn't have thought you could access array elements with the notation array[i][j] when it was alloced this way, but apparently you can do that too. Seems this is quite a bit more efficient than what I have been doing up to now.

    You learn something new every day

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One could even get it down to one malloc if you allocated a chunk big enough for the array of pointers plus the array of items, and then set those pointers in the array to point to further along in the block where each row started.

    However, if one cares that much about memory locality and compactness, it's probably better to simply simulate a 2D array bitmap-style, where there's no need to allocate pointers at all.
    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"

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I was actually thinking of previous programs I have written where I need to realloc large arrays many times within a loop. It might save a bit of time for larger data sets. Then again, I have no idea how much computation time malloc and related functions actually take, so it might be a useless optimization. I'll have to try and see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Detecting memory leak in complicating source code
    By mosxopul in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2009, 11:41 AM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  4. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  5. Memory leak by simple char
    By Ktulu in forum C++ Programming
    Replies: 42
    Last Post: 11-05-2006, 01:59 PM