Skip to main content
5 votes
Accepted

Memory leak in Pset4

Actually, your apparent problem is that you are running valgrind against a different executable binary, also called resize. ~/workspace/ $ which resize /usr/bin/resize ~/workspace/ $ whatis resize ...
hotwebmatter's user avatar
  • 3,050
3 votes
Accepted

speller valgrind "uninitialized error was created by heap allocation"

Your new_node -> next is not initialised if it's the first node of a list. malloc allocates memory, but does not guarantee any content, you will get whatever was there before (likely all zeros if ...
Blauelf's user avatar
  • 21k
3 votes
Accepted

Pset5 Speller Valgrind error

Without seeing more code, it's hard to know. A leak of 568 bytes in 1 block usually indicates a file that wasn't closed. Is it possible that there's a return command executed before the dictionary ...
Cliff B's user avatar
  • 69k
3 votes
Accepted

CS50 pset5 Memory Leak (trie structure)

for (int i = 0; i < 26; i++) You have 27 elements (a-z and '), so <=26 or <27 would be appropriate.
Blauelf's user avatar
  • 21k
2 votes
Accepted

GetString() & Dynamic Memory Allocation

Yes it's true that for simplicity's sake, in the first psets, we din't free() the strings allocated by GetString() and that caused memory leaks. Nevertheless, most modern operating systems can see ...
ChrisG's user avatar
  • 7,396
2 votes

Pset5 unload trie 244 bytes lost

Your code isn't freeing recursively. It should keep on going down the tree while children[i]!=NULL, only freeing the node when that condition isn't met anymore. It then returns up the tree, freeing ...
ronga's user avatar
  • 1,415
2 votes
Accepted

char* and read-only memory

Yes and no. Yes it is stored to memory, but the way in which it is declared has no memory leaks (try making a test file and running valgrind to see for yourself). What does cause a memory leak is ...
NullityNull's user avatar
2 votes
Accepted

Valgrind memory leak, WORDS MISSPELLED: 1 pset5

Why are you mallocing space in check? Also, your free(checker) only runs if the word is not found. How about when the word is found? You return true and the function ends, so that free line is ...
curiouskiwi's user avatar
  • 18.7k
2 votes

How is my (custom) program leaking memory? I am preparing myself for pset5

You don't free the realloced memory. Your code buffer_temp_word = NULL; free(buffer_temp_word); means free is called for NULL. You should not change the value of ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

Speller hash table - unload memory not working

Well, here's a really interesting scenario! for (int i = 0; i < N; i++) { for (node *ptr = hashtable[i]; ptr != NULL; ptr = ptr->next) { free(ptr); nodes_freed++; } }...
Cliff B's user avatar
  • 69k
2 votes
Accepted

pset5 speller valgrind show leak in load function

Simply put, the unload function doesn't free anything. Worse, it loses the entire tree/trie. Here's why. First, pointer is created and initialized and set to NULL. Then the for loop starts. As the ...
Cliff B's user avatar
  • 69k
2 votes

speller valgrind "uninitialized error was created by heap allocation"

Blauelf is right. I had a similar problem which was being caused by not initialising the next field in the first node I was adding to the hash table. I fixed it by assigning NULL to the next field in ...
Matt Wise's user avatar
2 votes
Accepted

Pset5 SPELLER, CHECK50 errors and memory leak

Nothing after "MISSPELLED WORDS" in the check50 log indicates a segmentation fault. This line in unload while (cursor == NULL) is the culprit. It cannot free NULL. The equality test is wrong. The ...
DinoCoderSaurus's user avatar
2 votes

CS50 pset5 program is free of memory errors valgrind tests failed;

You are getting that error because in your 'load' function, you are inicializing the value 'next' here: temp->next = table[index]; inside an 'if' condition. If you also set temp->next to NULL after ...
Tritum's user avatar
  • 1,571
2 votes
Accepted

Pset5: strcasecmp doesn't return 0 when given the same string input

Perhaps there's a slight oversight in your code. Look at the following: lc_word[len + 1] = '\0'; Now, say that the word is "Cat". Length is 3 so len = 3. Remember that arrays start at 0,...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Speller Valgrind Issues

The problem lies in each of the malloc's in check and unload. All three node pointer vars that are created are used to process existing nodes, yet all three are initialized with malloc calls. They ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET 5 Speller fails memory check

I will add comments for any missing braces I add or indentation I change or for code I would change like so // NOTE blah blah blah #include <ctype.h> #include <stdbool.h> #include <...
UpAndAdam's user avatar
  • 1,456
2 votes

How to properly free memory in the following W3Schools c programming code demonstrating Queue Implementation using Linked Lists

Don't modify any of their methods. The fix is entirely in main You free the memory createQueue allocated into newQueue by free'ing newQueue at the end. Before you do that you use the dequeue function ...
UpAndAdam's user avatar
  • 1,456
1 vote
Accepted

Help with unloading my dic

Let's say the program loaded a dictionary of two words, "an" and "at". How many times will it try to free(hashtable[0])? By my accounting, twice, because the only node that gets free'd is hashtable[x]....
DinoCoderSaurus's user avatar
1 vote

Unknown memory leak on "check function" - CS50 Speller

Your code node *checker = malloc(sizeof(node)); /* ... */ checker = hashtable[index]; means you allocate a new node, and then you throw away the only reference to that node, leaving this ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

PSET4 unload() don't work

What makes you think that the problem lies with unload? What's your thought process that leads you there? Or did you jump to that conclusion because that's where all the freeing is done? (Teachable ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

56 Bytes of leaked memory in speller.c pset5

In load, remove free(nodenew);. Completely, as requested by accepted answer https://cs50.stackexchange.com/a/30559/12099. The node is still in use. If that doesn't solve the issue, add more data to ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

pset5 speller unload (trie) leaking memory

You need a second check in that large condition ((c == '\'') && (trav -> children[26] == NULL) || (c != '\'') && (trav -> children[c - 'a'] == NULL)) as it might happen that c =...
Blauelf's user avatar
  • 21k
1 vote

How can I ensure I'm changing the reference, not a copy, in this C function?

Pass a pointer to the thing you want to change. If the thing is a pointer to a node, pass a pointer to this pointer to a node, using & operator. You will copy the pointer to the pointer, but that'...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Pset5 Memory leak with trie scructure

unloading is fine. load isn't. free(current); refers to a node that's installed in your trie, so you may not free it. Remove that line. current -> is_word = true; is the only thing that should be ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Why C doesn't check for memory leaks automatically?

The compiler trusts the programmer. Some code might result in a memory leak depending on how you use it. Like malloc. If you look at the code, you would not know whether it results in a memory leak. ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Pset 5 - valgrind errror because of memory leak

The problem lies in these two lines and the usage of fgets. char *ptr = malloc(sizeof(char) * (LENGTH + 2)); for((ptr = fgets(ptr, 47, dictfile)); ptr != NULL; (ptr = fgets(ptr, 47, dictfile))) { ......
Cliff B's user avatar
  • 69k
1 vote

pset5. Can't trace memory leak

Function fopen() allocate memory for itself. Try without fclose(). You'll get leak of memory. You opened dictionary file and speller opened text file. So amount of memory will be = amount of nodes + ...
Silkov Evgen's user avatar
1 vote
Accepted

pset5. Can't trace memory leak

Prepare to slap head with hand! If you were to run valgrind with the full dictionary and a large file, you'd probably see thousands of blocks lost. So, given this declaration, typedef struct node { ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset5. Can unload words in dictionary but memory is definitely and indirectly lost

fclose the dict at the end of load, the file should be open only for the duration of reading. Otherwise, unload looks fine. In your hash function return (c != '\'')? c - 'a' : CHILDREN_LENGTH; ...
Blauelf's user avatar
  • 21k

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