19

The function detectCollision() returns NULL if there's no collision happened. You have to check whether your object is equal to NULL before you pass it to getType().


6

Should I declare freenodes in the header file? It is not necessary to declare freenodes in the header file. However because freenodes is written after the function which calls it, it should be declared at the top of the .c file, like so: void freenodes(node* path); This is the same as the function signature used to define the function itself, with the ...


3

See the C FAQ, Question 1.32 Q: What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A: A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: As the ...


3

The description of the GetString() function clearly states that it can return NULL on an error or an EOF. If you pass the return value to strlen() without checking, your program will crash. string s = GetString(); int stringlength = 0; if (s != 0) stringlength = strlen(s); This at least won't crash. I also observe that the code fragment: // ...


2

You can jump to SUGGESTION for quick answer. I plugged your check() function on my dictionary.c program. I also have a trie in my dictionary.c. I don't know what is causing the segmentation fault. However, I found a behaviour that might be of your interest. I ran the program with both my check() function and yours against alice.txt. I found that among the ...


2

This is a really tricky mistake happened around line 50-53. A live test of your code is available here: segfault fixed, I made a fix at line 52-53 and commented out the faulty code. It works correctly. An explanation of the bug: at line 50 node * node = NULL; at line 53 node = malloc(sizeof(node)); here node in sizeof(node) is not the node type, ...


2

Ok, I've found answer to c segmentation fault on tail recursion: changing clang option -o from 0 to 3 fixes this problem as this enables better optimization: clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O3 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow crack.c -lcrypt -lcs50 -lm -o crack


2

if(!offset) offset = 0; does nothing. 0 is the value of false, while anything different could be interpreted as true in many contexts (even if it does not equal true, which is 1). With word[offset=='z'], you probably meant word[offset]=='z'? I haven't completely understood your algorithm, but as there are 52^4+52^3+52^2+52^1 (+1 for empty string) different ...


1

The program reads one byte at a time and essentially appends it to buffer. So the number of bytes that buffer needs to hold increases each read. This buffer = realloc(buffer, sizeof(char)); allocates buffer for 1 byte every time. It needs to be at least as big as the number of bytes read. You're already keeping track of that so it should be an easy ...


1

There is a serious logic problem here. The load code only allocates one node, before the start of the while loop. All words are going to be added to that one node. (I leave it to you to study exactly how words are being assigned after the first one.) The code needs to allocate a new node for each word. Any remaining issues could simply be a result of this ...


1

The while executes an fread followed immediately by an fread. What is likely happening is: program never finds a jpeg signature because it is only processing every other block. Therefore, no img file is opened, therefore the close gives a seg fault.


1

The random values you see in word is the result of previous iterations of the loop assigning longer words than the current one. It's not an issue per se since the functions handling it (such as strlen or strcpy) will just read to the first null terminator anyway. The problem lies in the hash function, and actually took me a little while to figure out. It's ...


1

For starters, you can't free table[i] and two lines later try to access table[i]->next. It no longer exists. That's where the seg fault comes from. This unload code is needlessly complicated. I would suggest throwing it out and starting fresh. The pseudocode is simple. Is the pointer null? If yes, terminate the loop. Save the ->next pointer. delete ...


1

Two problems. First, table[] was never initialized. Second, the if statement in unload allows i to be 27, which means that table[27] is selected, but doesn't exist. There may be other issues, but these two jumped out at me. [Edit] There's a third problem. The do/while loop in load will try to process a word read from the dictionary, but since it's a do/...


1

There was a little bit of code missing, so I can't tell exactly why you're getting the seg fault for sure, but I'm 99% certain of the problem. The code is treating word_count as a pointer. Why? Just create a standard int var as a global var, set it to 0, and use it to count. No *'s, no &'s and no malloc.


1

Having a look at your code was really helpful, I was having a hard time with this problem because I cant fully understand how fread works. You keep reading rawPtr and sending it to buffer, and the size of it to bytesRead, but I dont understand why it wont read the same block of 512B, you are not telling the program to go to the next block of 512, I suppose ...


1

Your code is very nice and neat congratulations. Your problem resides in your hash function. When passed a string of less than 3 caracters returns a negative value, so when you try to use it as index in your load() function it yells at you :) You got it right. The array table[] are just pointers to nod structs.


1

It could be because you're looking in the wrong place. The code is seg faulting in the unload() function. Specifically, it fails at this line: child[i] = curr->children[i]; The seg fault happens because curr is uninitialized. Also, why does unload() call itself recursively at the end, with no mechanism to terminate? If this answers your question, ...


1

The first thing I noticed is that the hashtable array is not allocated here hashtable[i] = NULL;. Remember, each index can hold a node. I fixed that in my repro case, but the error persisted. I created a small two-word dictionary (which of course did not seg fault), and ran it through valgrind (as both dictionary and text). It reported an Invalid write of ...


1

while(isspace(name[length - i]) && w == true) { ++i; } This loop is the culprit. when i increases, and length - i decreases. At some iterations length-i will become a negative value, and you get segfault.


1

You forget to fclose the dictionary, but else... l_node->next = NULL; would be a bad idea before testing if l_node == NULL. Make sure your buffer variable and word property both can handle a word of the maximum length (remember the final null terminator, so at least char[LENGTH+1]). Also, ensure hash_function returns numbers in the right range for ...


1

Apostrophe problem. Apostrophe is 39 in ascii. (96 is backwards apostrophe, or back-tick). When check encounters apostrophe here if (word[c]<91) translated=word[c]-65; translated is a negative number. There is the same problem in load.


1

Two things. One. Not sure why you say "not in the CS50 library" when you then pasted in the exact function that is in the CS50 library. Two. You'd generally get a segfault with strlen if the string doesn't actually exist (it's NULL) at the time you call strlen(). Are you actually entering a string in the terminal in response to your call to GetString()? ...


1

The problem was fileName array, it needs to be 8, not 7. sprintf was overwriting important memory because of that.


1

You were on the right track. buckets is declared as an array of pointers, but they're not initialized, so they contain garbage data. They do need to be initialized. Next, the compiler is choking because of where you are trying to initialize buckets. Only certain actions, like declaring a global variable are allowed to be done outside of main or a function. ...


1

Remember that you can't change the length of a string array once it has been created in c. Now look at the following: char requestTarget[] = ""; char query[] = ""; This code creates a couple of really short strings that you later try to copy a lot more data into. This is almost certainly causing the seg faults. If this answers your question, ...


1

use tools like gdb and valgrind to detect the cause of the problem and solve it! your load function: also do you think relying on feof may cause you to allocate more memory than you need? are you sure the member named word in your struct has enough space to store the current word? just else is enough as oppose to else if (hashtable[index] != NULL). see why?...


1

Your segmentation fault is due to the following line: if(isalpha(argv[1]))//checks if key is letters isalpha() tests to see if a single character is an alpha. It does not operate on whole strings. When you try to stuff a string into it, it blows up. You need to run a loop across the string, checking each element in argv, i.e., argv[1][i]. As a side note, ...


1

Yes you are correct. The compiler knows what data type you are using and traverses by the correct amount of bytes every time. You just tell it how many places away from zero you want (by the + i). You can take a look at the example shown here: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html


1

The string hasn't been given space on the stack. What is on the stack is a pointer called phrase. In the switch statement, they are setting this pointer to point to one of the string constants (like "Bad Request"). These string constants are stored as part of the executable file when you compile. (If you were to look at the server executable using a hex ...


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