Guest User

CS50 PSET5 Mem leak

a guest
Aug 8th, 2017
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Implements a dictionary's functionality.
  3. */
  4. #include <cs50.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include <ctype.h>
  11.  
  12. #include "dictionary.h"
  13.  
  14. #define ASCIIA 96
  15.  
  16. //typedefs
  17. typedef struct node
  18. {
  19. bool isword;
  20. struct node *letter[27]; //letter[0] corresponds with apos
  21. }node;
  22.  
  23. //global vars
  24. int wordcount = 0;
  25. node *root = NULL;
  26.  
  27. //prototypes
  28. void insert(char *newword);
  29. void byebye(node *delnode);
  30.  
  31. /**
  32. * Returns true if word is in dictionary else false.
  33. */
  34. bool check(const char *word)
  35. {
  36. node *ptr = root;
  37. char curchar;
  38. int wordlength = strlen(word);
  39. for(int i = 0; i < wordlength; i++)
  40. {
  41. curchar = tolower(word[i]);
  42.  
  43. if(ptr->letter[(int)curchar-ASCIIA]==NULL)
  44. return false;
  45. ptr = ptr->letter[(int)curchar-ASCIIA];
  46.  
  47. }
  48.  
  49. if(ptr->isword == true)
  50. return true;
  51. return false;
  52. }
  53.  
  54. /**
  55. * Loads dictionary into memory. Returns true if successful else false.
  56. */
  57. bool load(const char *dictionary)
  58. {
  59.  
  60. root = malloc(sizeof(node));
  61. if(root == NULL)
  62. return false;
  63. for(int i = 0; i < 26; i++)
  64. {
  65. root->letter[i] = NULL;
  66. }
  67. root->isword = false;
  68.  
  69.  
  70. FILE* fp = fopen(dictionary, "r");
  71.  
  72. // check for an unsuccessful open
  73. if (fp == NULL)
  74. return false;
  75.  
  76. // set a buffer to store an output word of length = LENGTH + 1 for null terminator
  77. char curline[LENGTH+2];
  78.  
  79. // loop through the dictionary until a null character
  80. while (fgets(curline, sizeof(curline), fp))
  81. {
  82. char c = '\0';
  83. curline[strlen(curline)-1] = c;
  84. insert(curline);
  85. wordcount++;
  86. }
  87.  
  88. fclose(fp);
  89. return true;
  90. }
  91.  
  92.  
  93.  
  94. /**
  95. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  96. */
  97. unsigned int size(void)
  98. {
  99. // TODO
  100. return wordcount;
  101. }
  102.  
  103. /**
  104. * Unloads dictionary from memory. Returns true if successful else false.
  105. */
  106. bool unload(void)
  107. {
  108. node *ptr = root;
  109. byebye(ptr);
  110.  
  111. return true;
  112. }
  113.  
  114. void insert(char *newword)
  115. {
  116. int length = strlen(newword);
  117. node *curptr = root;
  118. for(int i = 0; i < length; i++)
  119. {
  120. if(curptr->letter[(int)newword[i]-ASCIIA] == NULL)
  121. curptr->letter[(int)newword[i]-ASCIIA] = malloc(sizeof(node));
  122.  
  123. if(i+1 == length)
  124. curptr->letter[(int)newword[i]-ASCIIA]->isword = true;
  125.  
  126. //advance to next letter
  127. curptr = curptr->letter[(int)newword[i]-ASCIIA];
  128. }
  129. }
  130.  
  131.  
  132. void byebye(node *delnode)
  133. {
  134.  
  135. node *ptr = delnode;
  136.  
  137. for(int i = 0; i < 27; i++)
  138. if(ptr->letter[i] != NULL)
  139. byebye(ptr->letter[i]);
  140.  
  141. free(delnode);
  142.  
  143. }
  144. -----------------------------------------------------------------------
  145. ==1737== Memcheck, a memory error detector
  146. ==1737== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  147. ==1737== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
  148. ==1737== Command: ./speller texts/austinpowers.txt
  149. ==1737==
  150. ==1737== Conditional jump or move depends on uninitialised value(s)
  151. ==1737== at 0x40132B: insert (dictionary.c:120)
  152. ==1737== by 0x40128D: load (dictionary.c:84)
  153. ==1737== by 0x40095D: main (speller.c:40)
  154. ==1737== Uninitialised value was created by a heap allocation
  155. ==1737== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  156. ==1737== by 0x40133C: insert (dictionary.c:121)
  157. ==1737== by 0x40128D: load (dictionary.c:84)
  158. ==1737== by 0x40095D: main (speller.c:40)
  159. ==1737==
  160. ==1737== Use of uninitialised value of size 8
  161. ==1737== at 0x401322: insert (dictionary.c:120)
  162. ==1737== by 0x40128D: load (dictionary.c:84)
  163. ==1737== by 0x40095D: main (speller.c:40)
  164. ==1737== Uninitialised value was created by a heap allocation
  165. ==1737== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  166. ==1737== by 0x40133C: insert (dictionary.c:121)
  167. ==1737== by 0x40128D: load (dictionary.c:84)
  168. ==1737== by 0x40095D: main (speller.c:40)
  169. ==1737==
  170. ==1737== Invalid read of size 8
  171. ==1737== at 0x401322: insert (dictionary.c:120)
  172. ==1737== by 0x40128D: load (dictionary.c:84)
  173. ==1737== by 0x40095D: main (speller.c:40)
  174. ==1737== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
  175. ==1737==
  176. ==1737==
  177. ==1737== Process terminating with default action of signal 11 (SIGSEGV)
  178. ==1737== Access not within mapped region at address 0xA1
  179. ==1737== at 0x401322: insert (dictionary.c:120)
  180. ==1737== by 0x40128D: load (dictionary.c:84)
  181. ==1737== by 0x40095D: main (speller.c:40)
  182. ==1737== If you believe this happened as a result of a stack
  183. ==1737== overflow in your program's main thread (unlikely but
  184. ==1737== possible), you can try to increase the size of the
  185. ==1737== main thread stack using the --main-stacksize= flag.
  186. ==1737== The main thread stack size used in this run was 8388608.
  187. ==1737==
  188. ==1737== HEAP SUMMARY:
  189. ==1737== in use at exit: 78,291,032 bytes in 349,512 blocks
  190. ==1737== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
  191. ==1737==
  192. ==1737== LEAK SUMMARY:
  193. ==1737== definitely lost: 0 bytes in 0 blocks
  194. ==1737== indirectly lost: 0 bytes in 0 blocks
  195. ==1737== possibly lost: 0 bytes in 0 blocks
  196. ==1737== still reachable: 78,291,032 bytes in 349,512 blocks
  197. ==1737== suppressed: 0 bytes in 0 blocks
  198. ==1737== Reachable blocks (those to which a pointer was found) are not shown.
  199. ==1737== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  200. ==1737==
  201. ==1737== For counts of detected and suppressed errors, rerun with: -v
  202. ==1737== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
  203. Segmentation fault
  204. ~/workspace/pset5/speller/ $ make
  205. clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o speller.o speller.c
  206. clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o dictionary.o dictionary.c
  207. clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -o speller speller.o dictionary.o
  208. ~/workspace/pset5/speller/ $ valgrind ./speller texts/austinpowers.txt
  209. ==1814== Memcheck, a memory error detector
  210. ==1814== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  211. ==1814== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
  212. ==1814== Command: ./speller texts/austinpowers.txt
  213. ==1814==
  214. ==1814== Conditional jump or move depends on uninitialised value(s)
  215. ==1814== at 0x40132B: insert (dictionary.c:120)
  216. ==1814== by 0x40128D: load (dictionary.c:84)
  217. ==1814== by 0x40095D: main (speller.c:40)
  218. ==1814== Uninitialised value was created by a heap allocation
  219. ==1814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  220. ==1814== by 0x40133C: insert (dictionary.c:121)
  221. ==1814== by 0x40128D: load (dictionary.c:84)
  222. ==1814== by 0x40095D: main (speller.c:40)
  223. ==1814==
  224. ==1814== Use of uninitialised value of size 8
  225. ==1814== at 0x401322: insert (dictionary.c:120)
  226. ==1814== by 0x40128D: load (dictionary.c:84)
  227. ==1814== by 0x40095D: main (speller.c:40)
  228. ==1814== Uninitialised value was created by a heap allocation
  229. ==1814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  230. ==1814== by 0x40133C: insert (dictionary.c:121)
  231. ==1814== by 0x40128D: load (dictionary.c:84)
  232. ==1814== by 0x40095D: main (speller.c:40)
  233. ==1814==
  234. ==1814== Invalid read of size 8
  235. ==1814== at 0x401322: insert (dictionary.c:120)
  236. ==1814== by 0x40128D: load (dictionary.c:84)
  237. ==1814== by 0x40095D: main (speller.c:40)
  238. ==1814== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
  239. ==1814==
  240. ==1814==
  241. ==1814== Process terminating with default action of signal 11 (SIGSEGV)
  242. ==1814== Access not within mapped region at address 0xA1
  243. ==1814== at 0x401322: insert (dictionary.c:120)
  244. ==1814== by 0x40128D: load (dictionary.c:84)
  245. ==1814== by 0x40095D: main (speller.c:40)
  246. ==1814== If you believe this happened as a result of a stack
  247. ==1814== overflow in your program's main thread (unlikely but
  248. ==1814== possible), you can try to increase the size of the
  249. ==1814== main thread stack using the --main-stacksize= flag.
  250. ==1814== The main thread stack size used in this run was 8388608.
  251. ==1814==
  252. ==1814== HEAP SUMMARY:
  253. ==1814== in use at exit: 78,291,032 bytes in 349,512 blocks
  254. ==1814== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
  255. ==1814==
  256. ==1814== LEAK SUMMARY:
  257. ==1814== definitely lost: 0 bytes in 0 blocks
  258. ==1814== indirectly lost: 0 bytes in 0 blocks
  259. ==1814== possibly lost: 0 bytes in 0 blocks
  260. ==1814== still reachable: 78,291,032 bytes in 349,512 blocks
  261. ==1814== suppressed: 0 bytes in 0 blocks
  262. ==1814== Reachable blocks (those to which a pointer was found) are not shown.
  263. ==1814== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  264. ==1814==
  265. ==1814== For counts of detected and suppressed errors, rerun with: -v
  266. ==1814== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
  267. Segmentation fault
  268. ~/workspace/pset5/speller/ $ valgrind ./speller texts/quote.txt
  269. ==1859== Memcheck, a memory error detector
  270. ==1859== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  271. ==1859== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
  272. ==1859== Command: ./speller texts/quote.txt
  273. ==1859==
  274. ==1859== Conditional jump or move depends on uninitialised value(s)
  275. ==1859== at 0x40132B: insert (dictionary.c:120)
  276. ==1859== by 0x40128D: load (dictionary.c:84)
  277. ==1859== by 0x40095D: main (speller.c:40)
  278. ==1859== Uninitialised value was created by a heap allocation
  279. ==1859== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  280. ==1859== by 0x40133C: insert (dictionary.c:121)
  281. ==1859== by 0x40128D: load (dictionary.c:84)
  282. ==1859== by 0x40095D: main (speller.c:40)
  283. ==1859==
  284. ==1859== Use of uninitialised value of size 8
  285. ==1859== at 0x401322: insert (dictionary.c:120)
  286. ==1859== by 0x40128D: load (dictionary.c:84)
  287. ==1859== by 0x40095D: main (speller.c:40)
  288. ==1859== Uninitialised value was created by a heap allocation
  289. ==1859== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  290. ==1859== by 0x40133C: insert (dictionary.c:121)
  291. ==1859== by 0x40128D: load (dictionary.c:84)
  292. ==1859== by 0x40095D: main (speller.c:40)
  293. ==1859==
  294. ==1859== Invalid read of size 8
  295. ==1859== at 0x401322: insert (dictionary.c:120)
  296. ==1859== by 0x40128D: load (dictionary.c:84)
  297. ==1859== by 0x40095D: main (speller.c:40)
  298. ==1859== Address 0xa1 is not stack'd, malloc'd or (recently) free'd
  299. ==1859==
  300. ==1859==
  301. ==1859== Process terminating with default action of signal 11 (SIGSEGV)
  302. ==1859== Access not within mapped region at address 0xA1
  303. ==1859== at 0x401322: insert (dictionary.c:120)
  304. ==1859== by 0x40128D: load (dictionary.c:84)
  305. ==1859== by 0x40095D: main (speller.c:40)
  306. ==1859== If you believe this happened as a result of a stack
  307. ==1859== overflow in your program's main thread (unlikely but
  308. ==1859== possible), you can try to increase the size of the
  309. ==1859== main thread stack using the --main-stacksize= flag.
  310. ==1859== The main thread stack size used in this run was 8388608.
  311. ==1859==
  312. ==1859== HEAP SUMMARY:
  313. ==1859== in use at exit: 78,291,032 bytes in 349,512 blocks
  314. ==1859== total heap usage: 349,512 allocs, 0 frees, 78,291,032 bytes allocated
  315. ==1859==
  316. ==1859== LEAK SUMMARY:
  317. ==1859== definitely lost: 0 bytes in 0 blocks
  318. ==1859== indirectly lost: 0 bytes in 0 blocks
  319. ==1859== possibly lost: 0 bytes in 0 blocks
  320. ==1859== still reachable: 78,291,032 bytes in 349,512 blocks
  321. ==1859== suppressed: 0 bytes in 0 blocks
  322. ==1859== Reachable blocks (those to which a pointer was found) are not shown.
  323. ==1859== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  324. ==1859==
  325. ==1859== For counts of detected and suppressed errors, rerun with: -v
  326. ==1859== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)
  327. Segmentation fault
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×