22
votes
Memory overlap in C
I believe you mean memmove which takes care of memory overlapping as oppose to memset. but what is memory overlapping anyway?
suppose we have an array of 5 chars, where each char is a byte long
++++...
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, ...
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 ...
3
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. ...
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 ...
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 ...
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 *)...
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.
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 ...
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...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
// ...
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 ...
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 ...
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)...
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.
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
1
vote
Memory overlap in C
Lets see:
memset: sets a memory segment to a constant value, so, there is no "overlapping" possible here, because there is just a unique, contiguous, memory segment to "set".
memcpy: you are ...
1
vote
Dose anyone know where memory allocated in resize.c and copy.c?
Unless you've been doing any mallocs (which aren't really needed), my guess is it's because you're not closing all files that you've opened.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
memory × 74pset5 × 21
speller × 19
valgrind × 18
pset4 × 15
c × 8
memory-leak × 7
recover × 6
malloc × 6
allocation × 6
pointers × 5
dynamic × 5
dictionary × 4
array × 4
cs50x × 3
check50 × 3
error × 3
week4 × 3
general × 3
load × 2
filter × 2
unload × 2
blur × 2
function × 2
variables × 2