A very important difference between C and Java

  • Very important difference between Java and C:

      • Java has a built-in garbage collection mechanism

      • C (and most other programming languages) does not !!!

  • Consequence:

      • A C program (i.e.: you !!!) must de-allocate (= un-reserve) variables that have become garbage !!!

  • Important fact:

      • Missing memory de-allocation is a very difficult bug to track down

      • This bug will only manifest itself when a program runs a long time (and accummulate too much garbage)

How to de-allocate garbage in C

  • Suppose we have previously allocated memory for an object using malloc( ):

     #include <stdlib.h>  // malloc( ) is declared in stdlib.h
    
     struct Node  *p;
    
     // Allocate (= reserve) memory for a List object
     p = malloc( sizeof( struct Node ) ); 

  • To de-allocate (= unreserve) the previously allocated memory:

     #include <stdlib.h>  // free( ) is declared in stdlib.h
    
     free( p ) ; 

    I will use free( ) later when I discuss delete( ) a element from a linked list