2006-08-24

GList stuff

/*
  compile:
  gcc -Wall -g `pkg-config --cflags glib-2.0` main.c -o glist-test `pkg-config --libs glib-2.0`
*/

#include <glib.h>
#include <glib/gprintf.h>

void print_list( GList *list);
GList *free_elem( GList *list, gint data);

int main( int argc, char **argv)
{
  GList *list =NULL;
  gint i;

  for( i =1; i <8; i++)
    list =g_list_append( list, GINT_TO_POINTER( i));
  print_list( list);

  list =free_elem( list, 2);
  print_list( list);

  list =free_elem( list, 4);
  print_list( list);

  list =free_elem( list, 6);
  print_list( list);

  g_list_free( list);
  print_list( list);

  return 0;
}

void print_list( GList *list)
{
  GList *l;

  g_printf( "%p: ", list);

  for( l =list; l !=NULL; l =g_list_next( l))
    g_printf( "0x%x -> ", GPOINTER_TO_INT( l->data));
  g_printf( "NULL\n\n");
}

GList *free_elem( GList *list, gint data)
{
  GList *l =g_list_find( list, GINT_TO_POINTER( data));

  list =g_list_remove_link( list, g_list_find( list, GINT_TO_POINTER( data)));

  g_list_free_1( l);

  print_list( l);

  return list;
}
Running with:
valgrind --leak-check=yes --show-below-main=yes --show-reachable=yes ./glist-test
gives:
...

0x41df000: 0x1 -> 0x2 -> 0x3 -> 0x4 -> 0x5 -> 0x6 -> 0x7 -> NULL

0x41df010: 0x0 -> NULL

0x41df000: 0x1 -> 0x3 -> 0x4 -> 0x5 -> 0x6 -> 0x7 -> NULL

0x41df030: 0x41df010 -> NULL

0x41df000: 0x1 -> 0x3 -> 0x5 -> 0x6 -> 0x7 -> NULL

0x41df050: 0x41df030 -> NULL

0x41df000: 0x1 -> 0x3 -> 0x5 -> 0x7 -> NULL

0x41df000: 0x41df050 -> NULL

==5433== 
==5433== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==5433== malloc/free: in use at exit: 4,556 bytes in 8 blocks.
==5433== malloc/free: 8 allocs, 0 frees, 4,556 bytes allocated.
==5433== For counts of detected errors, rerun with: -v
==5433== searching for pointers to 8 not-freed blocks.
==5433== checked 83,028 bytes.
==5433== 
==5433== 992 bytes in 4 blocks are still reachable in loss record 1 of 2
==5433==    at 0x401C970: memalign (vg_replace_malloc.c:332)
==5433==    by 0x401C9FB: posix_memalign (vg_replace_malloc.c:384)
==5433==    by 0x4063068: (within /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x4063E20: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x404B8AB: g_list_append (in /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x80484DD: main (main.c:21)
==5433==    by 0x40C2EA3: __libc_start_main (in /lib/tls/libc-2.3.6.so)
==5433==    by 0x8048420: ??? (start.S:119)
==5433== 
==5433== 
==5433== 3,564 bytes in 4 blocks are still reachable in loss record 2 of 2
==5433==    at 0x401C7AA: calloc (vg_replace_malloc.c:279)
==5433==    by 0x4054FC9: g_malloc0 (in /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x4062CDF: (within /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x4063C1E: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x404B8AB: g_list_append (in /usr/lib/libglib-2.0.so.0.1000.3)
==5433==    by 0x80484DD: main (main.c:21)
==5433==    by 0x40C2EA3: __libc_start_main (in /lib/tls/libc-2.3.6.so)
==5433==    by 0x8048420: ??? (start.S:119)
==5433== 
==5433== LEAK SUMMARY:
==5433==    definitely lost: 0 bytes in 0 blocks.
==5433==      possibly lost: 0 bytes in 0 blocks.
==5433==    still reachable: 4,556 bytes in 8 blocks.
==5433==         suppressed: 0 bytes in 0 blocks.
So free_elem( ) does it's job..? Gott weiß.