A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

DavidUsa

  • one year ago

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

  • This Question is Closed
  1. DavidUsa
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @Jaynator495 Can you help me?

  2. DavidUsa
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @turingtest Please help

  3. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  4. DavidUsa
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes I know what a seg fault is.

  5. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Well, then what are you doing in the code? Without the code there is no way to tell where you are getting the fault.

  6. DavidUsa
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Towards the bottom of this. /**************************************************************************** * dictionary.c * * By David Hla * * Computer Science 50 * Problem Set 5 * * Implements a dictionary's functionality. ***************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <ctype.h> #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; }

  7. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  8. DavidUsa
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @rsmith6559 It's given in the pset that there will be 27 nodes.

  9. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    can you share a dictionary file?

  10. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  11. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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 */ }

  12. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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; }

  13. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  14. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  15. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.