@jaynator495 Getting a seg fault on pset5, can't figure out why.

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

@jaynator495 Getting a seg fault on pset5, can't figure out why.

edX CS50x Intro to CS I
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

@Jaynator495 Can you help me?
@turingtest Please help
Segmentation faults have to do with memory access violations. One of the common ones is when you try to access an element in an array that does not really have an allocation. Here is some faulty psudocode for a 5 posotion array: ``` int array[5] x = 5 while x > 0 array[x] = x x = x - 1 ``` This will fail the first time out. Why? Because in a 5 element array the positions are 0, 1, 2, 3, and 4. The code tries to start at spot 5, which does not exist, and causes an auto of range segmentation fault.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Yes I know what a seg fault is.
Well, then what are you doing in the code? Without the code there is no way to tell where you are getting the fault.
Towards the bottom of this. /**************************************************************************** * dictionary.c * * By David Hla * * Computer Science 50 * Problem Set 5 * * Implements a dictionary's functionality. ***************************************************************************/ #include #include #include #include #include #include "dictionary.h" int numberofwords; typedef struct trie { bool is_word; struct trie* children[27]; } trie; trie* start; trie file; /** * Frees nodes. Returns true if successful else false. */ bool freenode(trie* node); /** * Returns true if word is in dictionary else false. */ bool check(const char* word) { trie* root = start; int index; int length = strlen (word); for (int n = 0; n < length; n++) { if (isalpha(word[n])) { index = ( (int) word[n] ) % 32; } else { index = 0; } root = root -> children[index]; if (n == length) { if (root -> is_word == true) { return true; } } if (root -> children[index] == NULL) { return false; } } return false; } /** * Loads dictionary into memory. Returns true if successful else false. */ bool load(const char* dictionary) { trie* root = start; // open dictionary FILE* dictionaryfile = fopen(dictionary, "r"); // check for null if (dictionaryfile == NULL) { return false; } char buffer; while (fread (&buffer, sizeof(char), 1, dictionaryfile) == 1) { fseek (dictionaryfile, -(sizeof(char)), SEEK_CUR); char words[LENGTH]; fscanf(dictionaryfile, "%s", words); numberofwords++; int loadindex; int lengthofword = strlen(words); for (int i = 0; i < lengthofword; i++) { if(isalpha(words[i])) { loadindex = ( (int) words[i]) % 32; } else { loadindex = 0; } root = start -> children[loadindex]; if (start -> children[loadindex] == NULL) { trie* new_node = malloc (sizeof(trie)); new_node++; } } root -> is_word = true; fclose(dictionaryfile); return true; } return false; } /** * Returns number of words in dictionary if loaded else 0 if not yet loaded. */ unsigned int size(void) { if (numberofwords > 0) { return numberofwords; } return 0; } /** * Unloads dictionary from memory. Returns true if successful else false. */ bool unload(void) { freenode(start); return true; } /** * Frees nodes. Returns true if successful else false. */ bool freenode(trie* node) { for (int i = 0; i < 27; i++) { // recursively call freenode() for each malloc'd child if (node->children[i] != NULL) { freenode(node -> children[i]); } } // free the parent (this) node free(node); return true; }
I didn't really study your code, but a cursory read: in freenode(), having both iteration with a hard coded length and recursion, seems like a good place to look for memory issues.
@rsmith6559 It's given in the pset that there will be 27 nodes.
can you share a dictionary file?
this is why you're segfaulting: root = start -> children[loadindex]; if (start -> children[loadindex] == NULL) { trie* new_node = malloc (sizeof(trie)); new_node++; } when you come across that you haven't allocated this branch yet, you allocate one and then do nothing to it (new_node++; followed by never being used again), leaving root to still be NULL. outside your loop you hit root -> is_word = true; ... which causes your access violation, since you're trying to write to a some small offset from 0 which is definitely not writable memory in your process' valid memory space
presumably you meant to do something like this in your loop: for (int i = 0; i < lengthofword; i++) { if(isalpha(words[i])) { loadindex = ( (int) words[i]) % 32; } else { root->is_word = true; /* terminate the old word */ root = start; /* reset to head of tree for new word */ loadindex = 0; } if (root->children[loadindex] == NULL) { root->children[loadindex] = calloc(1, sizeof(trie)); /* if the child doesn't exist, allocate it with defaults */ } root = root->children[loadindex]; /* select the next node for this letter of the word */ }
another problem is that in check(const char *) you access root->children[index] without checking that root is null; in fact, you should probably check that *first* bool check(const char* word) { trie* root = start; int index; int length = strlen (word); for (int n = 0; n < length; n++) { if (isalpha(word[n])) { index = ( (int) word[n] ) % 32; } else { index = 0; } root = root -> children[index]; if (!root) { return false; } if (n == length) { if (root -> is_word == true) { return true; } } } return false; }
freenode was not the problem; iterating and recursion make perfect sense here, each node has a child per letter of the alphabet (+1, the unused index 0 since ASCII lowercase abc...z mod 32 map between 1 and 27) and then freeing each child node. check had another bug but your code doesn't get far enough for it to be the culprit. the problem is that you weren't actually storing the allocated new nodes while populating the trie and you weren't properly terminating each word -- look at my fixed code for an indicator as to why that doesn't work
note that you can't just malloc(sizeof(trie)) since that will not set all of the children to null pointers and it won't set is_word to false

Not the answer you are looking for?

Search for more explanations.

Ask your own question