Skip to main content
4 votes
Accepted

Invalid pointer error when using free()

The problem lies in fread(&storage, sizeof(char), 1, old_file); This line is storing the data read directly in the pointer, overwriting the address of the malloc'd memory. It isn't storing the ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET5 compiles but gives: "free(): double free detected in tcache 2" error on testing

This is a great lesson that a problem in one place is caused by a bug somewhere else. While the code in unload is mostly correct (there is a small flaw), the data that it's processing is flawed. If ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Pset 5: Speller - Double free or corruption (fasttop) error

Let's look carefully at this one. First, one has to figure out where the error occurs. Valgrind shows it to be line 148 in unload. node *tmp = table[i]->next; Since this isn't a free() ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Week 4 lecture: calling free() after malloc() removes destination contents

It seems that maybe you haven't grasped the exact purpose of the free() function. Yes, it needs to be called at some point after a malloc call. BUT, not before the code is done using the memory. When ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset5 - speller "double free detected" when I run check 50 but program does nothing during self tests

This is a bug well hidden in plain sight. ;-) If you traced the program in debug, it runs all the way through to the unload() function, inside the while loop. It's failing when it hits the end of the ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Lab5 free_family function

The various branches are mutually exclusive. Only one of them will execute the way you've written it. But you want to free both the parents and the person. And why check for NULL here? else if(p->...
Fuelled_By_Coffee's user avatar
1 vote
Accepted

Struggling with PSET5 Speller valgrind problems (& possibly more)

I see a few problems in the code that are causing problems. First, table[] is not initialized. That means that all the pointers in the array contain garbage data instead of NULL when the program ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Speller program runs accurately with memory leaks. When I add free() to correct memory leak, program stops executing

You cannot free the memory in load because it needs to be used throughout the entire program. It needs to wait until the program is done and then can be freed in unload. It also appears that you have ...
Cliff B's user avatar
  • 69k
1 vote

Speller program runs accurately with memory leaks. When I add free() to correct memory leak, program stops executing

You shouldn't free memory in load(). That's where unload() comes in. All of the memory allocated in the code should be freed in unload() only, except very specific cases (this was answered by a mentor ...
Matías Gómez Seeber's user avatar
1 vote
Accepted

Speller - not sure what's wrong

I believe the problem might be here: for (int i = 0; i < N; i++) { //temp points to what table points to temp = table[i]; //cursor points to what table points to cursor = table[i]; ...
Matías Gómez Seeber's user avatar
1 vote
Accepted

pset4 speller trie segmentation fault

In unload_node, change point = point->children[i]; unload_node(point); to just unload_node(point->children[i]); You should not change point ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Unload works on small dictionary, but not on large? (speller.c / hash table method)

Hehe, put the word "zebra" in your small dictionary and see if it still works! ;-) I'm assuming that hashtable[25] represents words that start with 'z'. I'll wager that your small dictionary ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset5 (trie) valgrind memory leak

Without even looking at anything but this: (if (start->child[i] != NULL)) I would ask if start actually exists at this point in the execution. I'll look at the code and edit my answer if/when I ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

PSET4: free(): invalid next size (fast)

I think I found the answer. The main problem was that I was setting the ppixelIndex variable out of the for loop and I was incrementing it way too much. So when the first iteration for the first row ...
Dement0's user avatar
  • 13
1 vote
Accepted

Pset5 Unload Giving Double Free or Corruption error

You have a tree structure (yes, a trie, but that's more a variation), and I don't see how you intend to free all nodes. One misconception you seem to have is that freeing something makes the pointer ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

How/when to free this memory? (pset6/server)

Does valgrind return the leak if it finds index.html or index.php? I would predict this indexes would leak memory only if neither of those files is "found". Generally speaking, one would not change ...
DinoCoderSaurus's user avatar
1 vote
Accepted

check50 fails on caesar.c when using free()

It appears that check50 is using a slightly older version of the CS50 Library than the CS50 IDE is currently using. In that version, all strings created using GetString are freed for you before the ...
curiouskiwi's user avatar
  • 18.7k
1 vote

Pset6 - CS50. Segmentation fault for free(index)

Looks like a small memory leak in loookup. Maybe you're declaring a variable to store extension, but it's not allocated to include a null-terminator? The indexes function looks fine. I think index is ...
DinoCoderSaurus's user avatar
1 vote
Accepted

per valgrind: multiple invalid read and invalid write; invalid free(); "still reachable: 568 bytes in 1 blocks"

You can use your recursive function, but to do so, you need to pass the FILE* target variable by reference, rather than by value as you have done. Recall Prof Malan's noswap.c example in Week 3. Or ...
curiouskiwi's user avatar
  • 18.7k
1 vote

Pset6 Indexes free() invalid next size error

sizeof (char), the sizeof operator returns the size in bytes of char, most likely to be 1, so that path_php has the same longuitud that path. Sure you know which is the mistake that you make.
MARS's user avatar
  • 5,179

Only top scored, non community-wiki answers of a minimum length are eligible