Advertisement
Guest User

ughh memory leaks

a guest
Aug 14th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. /**
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  */
  9.  
  10. #include <stdbool.h>
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "dictionary.h"
  16. #include <string.h>
  17.  
  18. typedef struct node
  19. {
  20.     bool isword;
  21.     struct node* children[27];
  22. }node;  
  23. unsigned int dSize = 0; // keeps track of the size of the dictionary
  24. node* head; // makes sure I can still use this node that way I can unload
  25. /**
  26.  * Returns true if word is in dictionary else false.
  27.  */
  28. bool check(const char* word)
  29. {
  30.     node* spider = head; // traversing the node
  31.     int charVal = -1;
  32.     if(word == NULL)
  33.     {
  34.         return false;
  35.     }
  36.     for(int i = 0; i < strlen(word);i++)
  37.        {
  38.            charVal = (tolower(word[i])-97);
  39.            if(charVal == 39)
  40.            {
  41.                charVal = 26;
  42.            }
  43.            if(spider ->children[charVal] != NULL)
  44.            {    
  45.                spider = spider -> children[charVal];
  46.                if(i == strlen(word)-1)
  47.                {
  48.                     if(spider ->isword == true)
  49.                     {
  50.                        return true;
  51.                     }
  52.                 }
  53.            }
  54.            else return false;
  55.        }
  56.     return false;
  57. }
  58.  
  59.  
  60. /**
  61.  * Loads dictionary into memory.  Returns true if successful else false.
  62.  */
  63. bool load(const char* dictionary)
  64. {
  65.     head = malloc(sizeof(node));
  66.     node* spider = head; // traversing the node
  67.     FILE *dict = fopen(dictionary,"r");
  68.     char word[LENGTH + 1]= {}; //for reading the words in the dictionary
  69.     int charVal = 0;
  70.     int app = 0;
  71.     if (dict == NULL)
  72.     {
  73.         return false;
  74.     }
  75.    
  76.     while(fscanf(dict, "%s\n", word) != EOF) // word is captured
  77.     {
  78.        spider = head;
  79.        app = 0;
  80.        for(int i = 0; i < strlen(word)+app;i++)
  81.        {
  82.            charVal = (tolower(word[i])-97);
  83.            if(charVal == 39)
  84.            {
  85.                app = 1;
  86.                charVal = 26;
  87.            }
  88.            if(spider -> children[charVal] == NULL)
  89.            {
  90.                 spider -> children[charVal] = malloc(sizeof(node));
  91.                 spider = spider -> children[charVal];
  92.                 if(i == strlen(word)-1)
  93.                 {
  94.                     dSize++;
  95.                     spider ->isword = true;
  96.                 }
  97.            }
  98.            else
  99.            {
  100.                spider = spider -> children[charVal];
  101.            }
  102.        }
  103.     }
  104.     fclose(dict);
  105.     return true;
  106. }
  107.  
  108. /**
  109.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  110.  */
  111. unsigned int size(void)
  112. {
  113.     return dSize;
  114. }
  115.  
  116. /*
  117.  * Unloads dictionary from memory.  Returns true if successful else false.
  118.  */
  119. bool freeStuff(node* spider) //recursive function to free
  120. {
  121.     for(int i = 0; i < 27; i++)
  122.     {
  123.         if(spider -> children[i] != NULL)
  124.         {
  125.             freeStuff(spider -> children[i]);
  126.         }
  127.     }
  128.     free(spider);
  129.     return true;
  130. }
  131. bool unload(void)
  132. {
  133.     return freeStuff(head);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement