Skip to main content
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 ...
ChrisG's user avatar
  • 7,396
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 ...
Cliff B's user avatar
  • 69.1k
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
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 ...
MARS's user avatar
  • 5,179
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 ...
Blauelf's user avatar
  • 21k
1 vote

Speller Valgrind leaks bytes + error: munmap_chunk(): invalid pointer error in C

This program does not compile in the IDE (or check50). First error in IDE: dictionary.c:28:13: error: implicit declaration of function 'hash' is invalid in C99 [-Werror,-Wimplicit-function-...
DinoCoderSaurus's 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 ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

PSET4 (Speller) malloc and calloc usage

When you copy over the word into your node, you forget to write a null terminator. The byte likely is initially zero, so your programme will probably work, but there's no guarantee. Using calloc ...
Blauelf's user avatar
  • 21k
1 vote

PSET4 free on malloc in hash function

str++ will increment str. So the thing you later try to free is pointing to the first byte behind the string's null terminator, while you are meant to call free for the first byte of the range, the ...
Blauelf's user avatar
  • 21k
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 ...
Cliff B's user avatar
  • 69.1k
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 ...
user3230142's user avatar
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 ...
Dement0's user avatar
  • 13
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

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?...
Cliff B's user avatar
  • 69.1k
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 ...
MARS's user avatar
  • 5,179
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).
DinoCoderSaurus's user avatar
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 ...
Cliff B's user avatar
  • 69.1k
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 ...
Cliff B's user avatar
  • 69.1k
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 ...
borker's user avatar
  • 301
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 ...
MARS's user avatar
  • 5,179
1 vote
Accepted

pset 4 Recover creates 50 corrupted or mainly grey images

First, the problem with the code lies in this line: fwrite(&buf, sizeof(BUFFER), 1, outptr); Adding the & is causing the problem. If you remove it, it will work. If you look at the man page ...
Cliff B's user avatar
  • 69.1k
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 ...
Peter's user avatar
  • 1,410
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 ...
Ryan McCauley's user avatar
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 ...
Blauelf's user avatar
  • 21k
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 ...
curiouskiwi's user avatar
  • 18.7k
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 ...
Yuri Laguardia's user avatar
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 ...
DinoCoderSaurus's user avatar
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));
Yuri Laguardia's user avatar
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 ...
Blauelf's user avatar
  • 21k
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 ...
curiouskiwi's user avatar
  • 18.7k

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