Thread: For some reason I end up with memory leak...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    21
    Quote Originally Posted by zacs7 View Post
    Code:
    void releaseMemory ( COUNTRY *list )
    {
    // Local Declarations
    	int i;
    
    // Statements
    	for ( i = 0; i <= 21; i++ )
    	{
    		free ( list[i].id );
    		free ( list[i].name );
    		free ( list[i].capital );
    		free ( list[i].population );
    	}
    
       return;
    }
    Where does this magic 21 come from? free()'ing non-NULL pointers that you didn't allocate is very bad.

    Also don't use gets() -- see the FAQ.
    I just put in 21 to see whether or not I freed the memory correctly. 21 is numCountries -1. The list's variables carry pointers to tempInt, tempName, tempCap, and tempPop. I also don't like using fgets in this situation because it carries over the '\n' as well.
    Last edited by RaDeuX; 11-25-2008 at 11:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  3. Memory leak prevention methodogies
    By c___newbie in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 11-17-2007, 03:45 AM
  4. memory leak
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 01:27 PM
  5. Is this a memory leak?
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2005, 01:15 PM