7
votes
Accepted
Pointers and arrays, don't understand the relationship
OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me.
Arrays vs Pointers
First, let's learn the difference between arrays and pointers. When you ...
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 ...
2
votes
Accepted
Recursion Destroy Singly linked List, memory leak?
You have only implemented part of your goal. This code will walk down a linked list to the last element, but will only free memory when it finds that the next element is null, i.e., it is at the end ...
2
votes
Accepted
declaring char array vs malloc
yes, indeed there is a difference! when you use malloc, the block of memory that you are trying to allocate gets allocated in a region of memory called the heap. freeing memory allocated on the heap ...
2
votes
Accepted
Want to make sure I understand struct, malloc, and pointers and how they relate to each other
I originally didn't alloc for the struct, just declared it. It worked,
and from what I understand, it worked because it was created in the
stack? Am I correct?
the type BITMAPINFOHEADER is not a ...
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
pset4 recover with malloc having a segmentation fault
char c[3];
is the guilty declaration of segfault, the array space is insufficient should be resolved with:
char c[8];
keep in mind that we must store something like 001.jpg which are seven ...
2
votes
Accepted
problem with recover.c using malloc and fread
You just opened your file for writing (and reading). This erased your card.raw's content.
The 0 you find in block[0] is the initial value of your allocated memory, which by coincidence is 0.
The 255 ...
1
vote
Accepted
Pset5 why calloc works and malloc doesn't ?
Without seeing the code involved, it's just a guess, but I'd bet that whatever you're allocating memory for has a pointer in it. Malloc only allocates memory without initializing anything, while ...
1
vote
PSET 4: Resize. Does not work with dynamic buffer, only static
Figured it out:
fwrite(rowBuff, sizeof(rowBuff), 1, outptr);
Doesn't work because sizeof called on dynamic array just returns the size of the pointer itself. Created an integer to store the ...
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 ...
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
PSet5 Hash Tables
It's always the simple ones that hide in plain sight. ;-) Look very carefully at this:
Node* temp = malloc(sizeof(Node*));
The code allocates memory space that is sizeof what? A node or a pointer?...
1
vote
pset2/crack/(Memory issue)
char *firstAlpha = (char *) malloc(sizeof(char));
for (int i = 0; i < 4; i++) {
*firstAlpha = intToChar(i);
printf("%s\n",firstAlpha);
addNewAlpha(firstAlpha);
I ...
1
vote
Accepted
for cicle itrations
Here's a problem: sizeof(name). name is a pointer, sizeof(a pointer) is 8. You probably want the length of name. (Think strlen included in string.h).
1
vote
Accepted
pset4 malloc() approach. Help with size of buffer
It has nothing to do with the malloc call itself. The problem lies in what you think you are checking. Look at the following:
RGBTRIPLE* row = ...
int sizeRow = sizeof(row);
row is declared as a ...
1
vote
PSET6 - Why is my filename string getting weird characters after two refreshes?
While this may not be the only problem, the math is "backwards". Remember, buffer3 is greater than buffer2 (found later in the string). So, buffer2 - buffer3 would not be the result you are after. ...
1
vote
Accepted
What will happen if you assign a static value to a pointer in C?
You will get the following message in CS50 IDE: error: incompatible integer to pointer conversion assigning to 'int *' from 'int'.
In the code above, pk is a pointer to an integer, not an integer ...
1
vote
Accepted
pset4 recover.c - working with fread() and malloc()
fread() reads the contents of a file (in your case 'card' is the pointer to the file to be read from) and places the contents into a buffer (in your case 'pattern'). The number of bytes placed into ...
1
vote
Accepted
Assigning an empty array with malloc has a random number in it
In the description for malloc, note the following sentence:
DESCRIPTION
Allocate size bytes of memory. Unlike calloc, malloc will not pre-set all allocated memory to zero.
This tells you what's ...
1
vote
Accepted
pset 4 recover - segmentation fault with malloc
First,
unsigned char *buffer = malloc(sizeof(buffer));
will give you a buffer of size 8 (because sizeof(buffer)) when buffer is a pointer is the size of a pointer (8 bytes). You need a buffer that ...
1
vote
Accepted
PSET 6 Problem with malloc() on parse function
This is what line looks like in memory, together with the space you're allocating for linecopy:
This could cause the erratic behavior you're seeing.
Other than that, you're extracting query and ...
1
vote
Accepted
pset6 parse function: Invalid read of size 1
This is a tough one. It has to do with reusing p and realloc. I cannot find a specific technical explanation, but regardless, there are things that are problematic with this approach.
First, you ...
1
vote
Pset5 - Problem with hash table load(), malloc, and segmentation-fault
What's causing your seg fault on this line is the sizeof operator:
newNode->word = malloc(sizeof(strlen(word) + 1));
What you really meant here was:
malloc((strlen(word) + 1) * sizeof(char));
1
vote
Accepted
Pset5 - Problem with hash table load(), malloc, and segmentation-fault
In check, you don't copy the terminator, you could add a tmpWord[wordLen] = 0; between the for- and the while loop.
It makes little to compare word and tmpWord (the lowercase version of word), but ...
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 ...
1
vote
Accepted
pset4 resize: how to copy a row
"Everyone's looking for a zebra. I see the injured horse."
All of the file reads and writes are using the outptr output file pointer. The only reference to the input file pointer inptr is the fclose ...
1
vote
Accepted
Segmentation fault in recover.c (pset4)
You have a misunderstaning of the return value from the fread() call. It will return the number of elements read - the third parameter. If it works correctly, the third parameter is returned. If it ...
1
vote
Segmentation fault in recover.c (pset4)
I'm working on the same pset but it seems like you have an infinite loop. Are you sure your first if statement is working correctly and ending when it reaches the EOF? Otherwise you'll just keep ...
1
vote
Kinda confused with what needs memory allocated in pset5
well, no where to start, first see the hash tablenode* hashtable[ALPHA], is an array of pointers to node, are pointers we will use to store our words according to their index hash, to equal hash index ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
malloc × 66pset4 × 19
pset5 × 11
speller × 10
recover × 9
pointers × 9
pset6 × 8
c × 8
segmentation-fault × 8
free × 8
valgrind × 6
memory × 6
resize × 5
structs × 5
load × 4
memory-leak × 4
cs50x × 3
dictionary × 3
array × 3
hash-table × 3
fread × 3
linked-list × 3
pset2 × 2
server × 2
trie × 2