Thread: Using free() to deallocate memory

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Using free() to deallocate memory

    Hello,
    I use a structure type that I want to remove from memory at the end of the program. I came up with something but since i'm new to C, I was wondering, does this code free all the memory used?
    Thanks in advance.

    Side question: is there any way to check the memory that my program uses, except for printing to the screen the value of the pointers and such? this way I could figure out by my self if my code is good.

    Code:
    struct _cmdline {
    	int	argc;
    	char**	argv;		// arguments variables (strings array)
    	char*	outRedir;	// output redirection file (NULL is none)
    	int	amp;
    }; typedef struct _cmdline cmdline;
    Code:
    void free_cmdline (cmdline** ptr){
    	int i;
    	for (i=0; i<(*ptr)->argc; i++){
    		free( (*ptr)->argv[i] );
    		(*ptr)->argv[i]=NULL;
    	}
    	free( (*ptr)->argv );
    	(*ptr)->argv=NULL;
    	
    	free( (*ptr)->outRedir );
    	(*ptr)->outRedir=NULL;
    	
    	free( *ptr );
    	(*ptr)=NULL;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So long as each pointer is allocated using malloc (or initialised to NULL), then that will free all the memory.

    As for your other question, consider using a tool like valgrind
    Eg.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main ( ) {
      char  *freed = malloc(10);
      char  *leaked = malloc(10);
      char  *broke = malloc(5);
      strcpy(broke,"hello");
      free(freed);
      free(broke);  // may or may not crash
      return 0;
    }
    
    $ valgrind ./a.out
    ==4733== Memcheck, a memory error detector
    ==4733== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==4733== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
    ==4733== Command: ./a.out
    ==4733== 
    ==4733== Invalid write of size 1
    ==4733==    at 0x40269B4: memcpy (mc_replace_strmem.c:497)
    ==4733==    by 0x80484A9: main (in /home/sc/work/a.out)
    ==4733==  Address 0x41990ad is 0 bytes after a block of size 5 alloc'd
    ==4733==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
    ==4733==    by 0x8048488: main (in /home/sc/work/a.out)
    ==4733== 
    ==4733== 
    ==4733== HEAP SUMMARY:
    ==4733==     in use at exit: 10 bytes in 1 blocks
    ==4733==   total heap usage: 3 allocs, 2 frees, 25 bytes allocated
    ==4733== 
    ==4733== LEAK SUMMARY:
    ==4733==    definitely lost: 10 bytes in 1 blocks
    ==4733==    indirectly lost: 0 bytes in 0 blocks
    ==4733==      possibly lost: 0 bytes in 0 blocks
    ==4733==    still reachable: 0 bytes in 0 blocks
    ==4733==         suppressed: 0 bytes in 0 blocks
    ==4733== Rerun with --leak-check=full to see details of leaked memory
    ==4733== 
    ==4733== For counts of detected and suppressed errors, rerun with: -v
    ==4733== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 8)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to deallocate space with free.
    By brack in forum C Programming
    Replies: 18
    Last Post: 08-27-2010, 01:41 AM
  2. deallocate pointer
    By sangit in forum C++ Programming
    Replies: 16
    Last Post: 03-13-2009, 01:08 PM
  3. Unable to deallocate memory for a tree structure.
    By broli86 in forum C Programming
    Replies: 5
    Last Post: 07-06-2008, 03:30 PM
  4. What can happen if you forget to deallocate memory?
    By avaldi in forum C++ Programming
    Replies: 19
    Last Post: 06-17-2008, 05:33 PM
  5. Deallocate Memory and reuse pointer
    By appleGuy in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2007, 11:07 AM