PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Valgrind and freeing command-line arguments
Date: Wednesday, 15 Apr 2020, 11:51:26


The command-line arguments (i.e., the string pointers in
the argv[] argument to main()) live on the stack, not in
the heap.  Thus you cannot free() them, not even if you
incorporate them in other data structures (e.g., hash
tables) with other blocks of storage that were allocated
in the heap.

When you try to free() them, valgrind will complain

  ==16407== Invalid free() / delete / delete[] / realloc()
  ==16407==    at 0x4839A0C: free (vg_replace_malloc.c:540)
  ==16407==    by 0x401FCD: HashDestroy (Hash.c:75)
  ==16407==    by 0x402162: grow (Hash.c:119)
  ==16407==    by 0x4024A0: HashInsert (Hash.c:218)
  ==16407==    by 0x4018C8: add_next (Nine20.c:180)
  ==16407==    by 0x4015B3: main (Nine20.c:111)
  ==16407==  Address 0x1fff00014a is on thread 1's stack

While the error message may seem cryptic, it just says
that you tried to free something that was not malloc()-ed,
in this case on the stack, and it should not be hard to
figure out what.

--Stan-
PREV INDEX NEXT