Hot answers tagged

6 votes
Accepted

How to free a nested struct?

When you allocate memory by calling malloc(), and you want to free that memory, you have to call free() on every pointer that you initialized it with a call to malloc(). For example, if you have ...
user avatar
  • 17.4k
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 ...
user avatar
  • 64.1k
3 votes
Accepted

Should We Free Memory Allocated By GetString()?

Short answer: yes! Long answer: looking at the code of GetString() string GetString(void) { // growable buffer for chars string buffer = NULL; // capacity of buffer unsigned int ...
user avatar
  • 17.4k
3 votes
Accepted

segfault while freeing memory in speller

You have to keep in mind that a node is only accessible through a previous node when dealing with a singly-linked-list. Also, since you've allocated memory separately for the string named word which ...
user avatar
  • 17.4k
2 votes
Accepted

pset6-unload free() : invalid pointer

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 ...
user avatar
  • 17.4k
2 votes

pset 05 trie large dictionary unload issue

@SFri: I agree that it's hard to tell without seeing some code, but I'll tell you what i think can happen from the errors and the pseudocode. The reason to set a recently freed pointer to null is to ...
user avatar
  • 1,677
2 votes
Accepted

Trie Spellchecker unload() - two allocs not freeing

You overlooked something. Here's a hint. What is the integer value of ' - 'a'? Or, subtract the ASCII value of 'a' from the value of an apostrophe? I'm actually surprised that your code isn't ...
user avatar
  • 64.1k
1 vote
Accepted

Is vmware free or chargeable?

As an alternative, you might try the open source VirtualBox, available at https://www.virtualbox.org/wiki/Downloads. It handles the CS50 appliance well.
user avatar
1 vote

Why can't I call the function "free" to release memory!

Per man free (3), The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). ...
user avatar
  • 17.4k
1 vote
Accepted

valgrind free and leaks

Pretty much. In very specific environments Application allocated memory may not be fully freed when the application exits. But it's safe to say that in any regular OS'es (including the one you're ...
user avatar
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 ...
user avatar
  • 64.1k
1 vote
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 ...
user avatar
  • 64.1k
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 ...
user avatar
  • 64.1k
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->...
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 ...
user avatar
  • 64.1k
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 ...
user avatar
  • 64.1k
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 ...
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]; ...
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 ...
user avatar
  • 20.7k
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 ...
user avatar
  • 64.1k
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 ...
user avatar
  • 64.1k
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 ...
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 ...
user avatar
  • 20.7k
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 ...
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 ...
user avatar
  • 18.4k
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 ...
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 ...
user avatar
  • 18.4k
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.
user avatar
  • 5,172

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