Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdbool.h>
- #include <ctype.h>
- #include "dictionary.h"
- #define ASCIIA 96
- //typedefs
- typedef struct node
- {
- bool isword;
- struct node *letter[27]; //letter[0] corresponds with apos
- }node;
- //global vars
- int wordcount = 0;
- node *root = NULL;
- //prototypes
- void insert(char *newword);
- void byebye(node *delnode);
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char *word)
- {
- node *ptr = root;
- char curchar;
- int wordlength = strlen(word);
- for(int i = 0; i < wordlength; i++)
- {
- curchar = tolower(word[i]);
- if(ptr->letter[(int)curchar-ASCIIA]==NULL)
- return false;
- ptr = ptr->letter[(int)curchar-ASCIIA];
- }
- if(ptr->isword == true)
- return true;
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- root = malloc(sizeof(node));
- if(root == NULL)
- return false;
- for(int i = 0; i < 26; i++)
- {
- root->letter[i] = NULL;
- }
- root->isword = false;
- FILE* fp = fopen(dictionary, "r");
- // check for an unsuccessful open
- if (fp == NULL)
- return false;
- // set a buffer to store an output word of length = LENGTH + 1 for null terminator
- char curline[LENGTH+2];
- // loop through the dictionary until a null character
- while (fgets(curline, sizeof(curline), fp))
- {
- char c = '\0';
- curline[strlen(curline)-1] = c;
- insert(curline);
- wordcount++;
- }
- fclose(fp);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return wordcount;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- node *ptr = root;
- byebye(ptr);
- return true;
- }
- void insert(char *newword)
- {
- int length = strlen(newword);
- node *curptr = root;
- for(int i = 0; i < length; i++)
- {
- if(curptr->letter[(int)newword[i]-ASCIIA] == NULL)
- curptr->letter[(int)newword[i]-ASCIIA] = malloc(sizeof(node));
- if(i+1 == length)
- curptr->letter[(int)newword[i]-ASCIIA]->isword = true;
- //advance to next letter
- curptr = curptr->letter[(int)newword[i]-ASCIIA];
- }
- }
- void byebye(node *delnode)
- {
- node *ptr = delnode;
- for(int i = 0; i < 27; i++)
- if(ptr->letter[i] != NULL)
- byebye(ptr->letter[i]);
- free(delnode);
- }
- -----------------------------------------------------------------------
- ==1737== Memcheck, a memory error detector
- ==1737== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==1737== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
- ==1737== Command: ./speller texts/austinpowers.txt
- ==1737==
- ==1737== Conditional jump or move depends on uninitialised value(s)
- ==1737== at 0x40132B: insert (dictionary.c:120)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737== Uninitialised value was created by a heap allocation
- ==1737== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1737== by 0x40133C: insert (dictionary.c:121)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737==
- ==1737== Use of uninitialised value of size 8
- ==1737== at 0x401322: insert (dictionary.c:120)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737== Uninitialised value was created by a heap allocation
- ==1737== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1737== by 0x40133C: insert (dictionary.c:121)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737==
- ==1737== Invalid read of size 8
- ==1737== at 0x401322: insert (dictionary.c:120)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
- ==1737==
- ==1737==
- ==1737== Process terminating with default action of signal 11 (SIGSEGV)
- ==1737== Access not within mapped region at address 0xA1
- ==1737== at 0x401322: insert (dictionary.c:120)
- ==1737== by 0x40128D: load (dictionary.c:84)
- ==1737== by 0x40095D: main (speller.c:40)
- ==1737== If you believe this happened as a result of a stack
- ==1737== overflow in your program's main thread (unlikely but
- ==1737== possible), you can try to increase the size of the
- ==1737== main thread stack using the --main-stacksize= flag.
- ==1737== The main thread stack size used in this run was 8388608.
- ==1737==
- ==1737== HEAP SUMMARY:
- ==1737== in use at exit: 78,291,032 bytes in 349,512 blocks
- ==1737== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
- ==1737==
- ==1737== LEAK SUMMARY:
- ==1737== definitely lost: 0 bytes in 0 blocks
- ==1737== indirectly lost: 0 bytes in 0 blocks
- ==1737== possibly lost: 0 bytes in 0 blocks
- ==1737== still reachable: 78,291,032 bytes in 349,512 blocks
- ==1737== suppressed: 0 bytes in 0 blocks
- ==1737== Reachable blocks (those to which a pointer was found) are not shown.
- ==1737== To see them, rerun with: --leak-check=full --show-leak-kinds=all
- ==1737==
- ==1737== For counts of detected and suppressed errors, rerun with: -v
- ==1737== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
- Segmentation fault
- ~/workspace/pset5/speller/ $ make
- clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o speller.o speller.c
- clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o dictionary.o dictionary.c
- clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -o speller speller.o dictionary.o
- ~/workspace/pset5/speller/ $ valgrind ./speller texts/austinpowers.txt
- ==1814== Memcheck, a memory error detector
- ==1814== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==1814== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
- ==1814== Command: ./speller texts/austinpowers.txt
- ==1814==
- ==1814== Conditional jump or move depends on uninitialised value(s)
- ==1814== at 0x40132B: insert (dictionary.c:120)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814== Uninitialised value was created by a heap allocation
- ==1814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1814== by 0x40133C: insert (dictionary.c:121)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814==
- ==1814== Use of uninitialised value of size 8
- ==1814== at 0x401322: insert (dictionary.c:120)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814== Uninitialised value was created by a heap allocation
- ==1814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1814== by 0x40133C: insert (dictionary.c:121)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814==
- ==1814== Invalid read of size 8
- ==1814== at 0x401322: insert (dictionary.c:120)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
- ==1814==
- ==1814==
- ==1814== Process terminating with default action of signal 11 (SIGSEGV)
- ==1814== Access not within mapped region at address 0xA1
- ==1814== at 0x401322: insert (dictionary.c:120)
- ==1814== by 0x40128D: load (dictionary.c:84)
- ==1814== by 0x40095D: main (speller.c:40)
- ==1814== If you believe this happened as a result of a stack
- ==1814== overflow in your program's main thread (unlikely but
- ==1814== possible), you can try to increase the size of the
- ==1814== main thread stack using the --main-stacksize= flag.
- ==1814== The main thread stack size used in this run was 8388608.
- ==1814==
- ==1814== HEAP SUMMARY:
- ==1814== in use at exit: 78,291,032 bytes in 349,512 blocks
- ==1814== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
- ==1814==
- ==1814== LEAK SUMMARY:
- ==1814== definitely lost: 0 bytes in 0 blocks
- ==1814== indirectly lost: 0 bytes in 0 blocks
- ==1814== possibly lost: 0 bytes in 0 blocks
- ==1814== still reachable: 78,291,032 bytes in 349,512 blocks
- ==1814== suppressed: 0 bytes in 0 blocks
- ==1814== Reachable blocks (those to which a pointer was found) are not shown.
- ==1814== To see them, rerun with: --leak-check=full --show-leak-kinds=all
- ==1814==
- ==1814== For counts of detected and suppressed errors, rerun with: -v
- ==1814== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
- Segmentation fault
- ~/workspace/pset5/speller/ $ valgrind ./speller texts/quote.txt
- ==1859== Memcheck, a memory error detector
- ==1859== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==1859== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
- ==1859== Command: ./speller texts/quote.txt
- ==1859==
- ==1859== Conditional jump or move depends on uninitialised value(s)
- ==1859== at 0x40132B: insert (dictionary.c:120)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859== Uninitialised value was created by a heap allocation
- ==1859== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1859== by 0x40133C: insert (dictionary.c:121)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859==
- ==1859== Use of uninitialised value of size 8
- ==1859== at 0x401322: insert (dictionary.c:120)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859== Uninitialised value was created by a heap allocation
- ==1859== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
- ==1859== by 0x40133C: insert (dictionary.c:121)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859==
- ==1859== Invalid read of size 8
- ==1859== at 0x401322: insert (dictionary.c:120)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
- ==1859==
- ==1859==
- ==1859== Process terminating with default action of signal 11 (SIGSEGV)
- ==1859== Access not within mapped region at address 0xA1
- ==1859== at 0x401322: insert (dictionary.c:120)
- ==1859== by 0x40128D: load (dictionary.c:84)
- ==1859== by 0x40095D: main (speller.c:40)
- ==1859== If you believe this happened as a result of a stack
- ==1859== overflow in your program's main thread (unlikely but
- ==1859== possible), you can try to increase the size of the
- ==1859== main thread stack using the --main-stacksize= flag.
- ==1859== The main thread stack size used in this run was 8388608.
- ==1859==
- ==1859== HEAP SUMMARY:
- ==1859== in use at exit: 78,291,032 bytes in 349,512 blocks
- ==1859== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
- ==1859==
- ==1859== LEAK SUMMARY:
- ==1859== definitely lost: 0 bytes in 0 blocks
- ==1859== indirectly lost: 0 bytes in 0 blocks
- ==1859== possibly lost: 0 bytes in 0 blocks
- ==1859== still reachable: 78,291,032 bytes in 349,512 blocks
- ==1859== suppressed: 0 bytes in 0 blocks
- ==1859== Reachable blocks (those to which a pointer was found) are not shown.
- ==1859== To see them, rerun with: --leak-check=full --show-leak-kinds=all
- ==1859==
- ==1859== For counts of detected and suppressed errors, rerun with: -v
- ==1859== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
- Segmentation fault
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement