Skip to main content
8 votes
Accepted

(pset5) valgrind - 1 block not freed

Hmmm..... 568 bytes in 1 block.... sounds like a file pointer. Did we forget to close an open file somewhere???? ;-) Side note: valgrind usually tells you how you can get more information, ...
Cliff B's user avatar
  • 69k
5 votes
Accepted

When we free a dynamically allocated memory, what happens to the pointer pointing at it?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address. It ...
Cliff B's user avatar
  • 69k
4 votes
Accepted

Pset5 Memory leak in unload()

Complicated code in terms of memory management. This set of problems deals with the traditionally more difficult concepts of C programming, so we need a good theoretical basis for dealing with them. ...
MARS's user avatar
  • 5,179
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
2 votes
Accepted

What will realloc() do if the contiguous memory location is already occupied?

The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment char *string; string = (char *)...
MARS's user avatar
  • 5,179
2 votes

What will realloc() do if the contiguous memory location is already occupied?

If realloc can't resize the memory block you pass in, it makes a new one, copies the data, and deallocates the old one. If I were you I'd read up a bit on specification for Realloc and malloc.
NullityNull's user avatar
2 votes
Accepted

Week 4, Section : Dynamic Memory Allocation

In addition to the points made by @MARS and @NullityNull, there are advantages and disadvantages to both methods. Allocating memory on the stack is more convenient because it automatically cleans up ...
hotwebmatter's user avatar
  • 3,050
2 votes

pset5 speller how to only use 7.4 kb memory?

You don't have to use the heap (dynamic allocation) nor the stack (local variables) to store the dictionary. You can declare a static data structure as global and house the dictionary in it. For ...
Yechiel Labunskiy's user avatar
2 votes
Accepted

pset5 speller how to only use 7.4 kb memory?

It depends on the metrics used, I think those are heap and stack memory. Global variables don't show up in those statistics. Now if you can estimate an upper limit for the size of the dictionary...
Blauelf's user avatar
  • 21k
2 votes
Accepted

Intuition for what is statically allocated memory vs dynamically allocated memory

It sounds like you've already got it, but you're just not all that confident in your grasp of the concepts. Since you have the mechanics down, maybe an analogy will help. Think of memory as your ...
Cliff B's user avatar
  • 69k
2 votes

Why Doesn't fwrite overwrite previously copied data?

From this article: One of the attributes of an open file is its file position that keeps track of where in the file the next character is to be read or written. On GNU systems, and all POSIX.1 ...
DinoCoderSaurus's user avatar
2 votes
Accepted

Pset4 - Memory/reflection problem

Think carefully about the values being used. Let's start with the first pixel, at 0,0, and that the image is 3x3 pixels. image[a][b].rgbtRed = image[a][width - b].rgbtRed; Fill in the numbers for the ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET4 Blur not passing Check50

Without doing a deep dive, I do see a major problem in the logic. The code copies all the pixels from image to temp array. But then, it does all the calculations using the temp array as the source ...
Cliff B's user avatar
  • 69k
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
1 vote
Accepted

Lab 4 problems when the audio is clearly altered

According to the spec: Your program should then read the rest of the data from the WAV file, one 16-bit (2-byte) sample at a time. This program is reading (and writing!) two 2-byte samples at a time....
DinoCoderSaurus's user avatar
1 vote
Accepted

Pointers Memory Allocation

Both name and number are declared as char * (ie, a pointer variable). Pointers hold addresses and the sizeof(char*) is 8 bytes in a 64-bit system like the CS50 IDE. So people will have a size of 16 ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Filter - Structures & Headers

The code knows how long each structure element is, thanks to the definition in the header file, so it only needs to add the appropriate number of bytes to the starting address to get to anything. The ...
Cliff B's user avatar
  • 69k
1 vote

PSET 5 SPELLER - Passing Valgrind but not Check50

The bug is in your unload function. You return after cleaning up just 1 list. But you have 26 of them to get through. The outer loop is also out of bounds. You iterate up to N + 1 for some reason. // ...
Fuelled_By_Coffee's user avatar
1 vote

Why Doesn't fwrite overwrite previously copied data?

The answer lies in how the file was opened. When opening a file with fopen (which must have been called earlier in the file), you can pass a parameter as the mode to open the file in. Some of these ...
Robert S. Pratt's user avatar
1 vote

dynamically allocated memory for a node is changed without any dereferencing?

You are printing tree->right the first time and &tree->right) the second time. So those won't be the same, since the first is the address stored in your variable tree->right and the ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Problem Set 4 Valgrind

The problem is in the declarations: char *checkword = malloc(sizeof(LENGTH + 1)); and i < strlen(word) + 1; Note that the length of the word array is LENGTH + 1, then the length of strlen (word)...
MARS's user avatar
  • 5,179
1 vote
Accepted

Speller (pset 4 2019) - Can't fix memory leak in hashtable

Have a look at your unload. You are returning true the first time the cursor is null. So your loop doesn't end up checking all of the table. Don't return true until the very end.
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Memory issue (Valgrind "Still reachable")

The problem is because of an oversight. I'm guessing that you started with 1000 buckets, but changed N to a much larger number. Unfortunately, instead of using N throughout your program, you ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Pset5 Valgrind Error: Reachable Memory

First, valgrind shows where the memory that is lost was allocated, not necessarily where the problem lies. ;-) Look at the following code: for (int i = 0; i < N; i++) { // No linked list at ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Speller, pset5. Help fix memory loss. How to fclose properly? How to close char *index = malloc(46)?

A couple of issues with your code: In your 'check' function you are trying to free() memory from a pointer in which you didn't allocate any memory. In fact, as it is pointing to the word found in ...
Tritum's user avatar
  • 1,571
1 vote

Errors from context when running trie dictionary from pset5 speller through valgrind

Memory allocated using malloc can initially contain anything, you would have to give the individual fields of your newly allocated struct values yourself. It still works because fresh memory usually ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Pset4 - Array declaration- Heap

Arrays you declare as int array[42]; are stored on stack, like any other variable. For arrays like int *array = (int*)malloc(42*sizeof(int));, a pointer array is stored on stack, the memory block of ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

pset 4 recover related question

You would have to read and interpret the partition, or device if it does not use partitions, not a file of the filesystem, as the file system driver might not provide any way to address "free" space ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Writing a word puzzle solver and having a problem with freeing heap memory. Would really appreciate some help!

When I tried to run your program, it ran for a long time before finally segfaulting. I put in a print statement to count the number of calls to your checkPermutations function, and it was more than 1....
curiouskiwi's user avatar
  • 18.7k

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