i get everything working but the unload function, there seems to be something wrong with free()

here' the code:

void freeChild(node* curr)
{   if(curr == NULL)
    for(int i = 0; i < 27; i++)

bool unload(void)
{   freeChild(&root);
    return true;

typedef struct node
{   bool is_end; 
    struct node* children[27];
} node;
node root = {false};

and it goes like this

*** Error in `./speller': free(): invalid pointer: 0x0804b04c ***
======= Backtrace: =========
======= Memory map: ========
08048000-0804a000 r-xp 00000000 fd:01 277991     /home/jharvard/Dropbox/pset6/speller
0804a000-0804b000 r--p 00001000 fd:01 277991     /home/jharvard/Dropbox/pset6/speller
0804b000-0804c000 rw-p 00002000 fd:01 277991     /home/jharvard/Dropbox/pset6/speller
09d12000-09d33000 rw-p 00000000 00:00 0          [heap]
41549000-41568000 r-xp 00000000 fd:01 401475     /usr/lib/ld-2.17.so
41568000-41569000 r--p 0001e000 fd:01 401475     /usr/lib/ld-2.17.so
41569000-4156a000 rw-p 0001f000 fd:01 401475     /usr/lib/ld-2.17.so
4156c000-41724000 r-xp 00000000 fd:01 401867     /usr/lib/libc-2.17.so
41724000-41725000 ---p 001b8000 fd:01 401867     /usr/lib/libc-2.17.so
41725000-41727000 r--p 001b8000 fd:01 401867     /usr/lib/libc-2.17.so
41727000-41728000 rw-p 001ba000 fd:01 401867     /usr/lib/libc-2.17.so
41728000-4172b000 rw-p 00000000 00:00 0 
417b9000-417d4000 r-xp 00000000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-    20130603.so.1
417d4000-417d5000 r--p 0001a000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-20130603.so.1
417d5000-417d6000 rw-p 0001b000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-20130603.so.1
b770f000-b7710000 rw-p 00000000 00:00 0 
b771f000-b7723000 rw-p 00000000 00:00 0 
b7723000-b7724000 r-xp 00000000 00:00 0          [vdso]
bfd57000-bfd78000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

will be really appreciate if someone could help!


The function free is used to free memory that was allocated on the heap using a function like malloc or calloc.

You declared root as a global variable of type node. Global variables are stored on the data segment in memory not on the heap.

  • but the other functions also use root, doesn't that mean i have to declare root as a global variable? or how can i fix the problem? – anonymous Oct 26 '14 at 12:27
  • @anonymous maybe you should declare root as a pointer to node and allocate memory for it. Or you may avoid freeing root at all because it's not on the heap. – Kareem Oct 26 '14 at 12:33
  • the problem seems to be solved! but another occurred... i received a segmentation fault (core dumped) from if(curr->children[tolower(word[i])-'a']!=NULL) in check() (node *curr; curr = root;) – anonymous Oct 26 '14 at 13:00
  • @anonymous make sure curr is not equal to NULL before trying to access curr->children[x]! – Kareem Oct 26 '14 at 13:16
  • i add it like this if(curr != NULL && curr->children[tolower(word[i])-'a']!=NULL) but segfault remains, and sometimes curr = curr->children[tolower(word[i])-'a']; gets segfault too. i also found that it runs well with small text but crushes when reading large ones(austinpowers.txt) – anonymous Oct 26 '14 at 13:46

Disclaimer: I haven't done this pset yet.

As Kareem pointed out, you end up free()'ing root in unload() by calling freeChild() on it.

Your 2 options are:

  1. malloc your root inside main() and then you can call freeChild on it.
  2. Change your unload() to only call freeChild on root's children.
  • thanks for your suggestion! i've just adapted the second option – anonymous Oct 27 '14 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .