Pārlūkot izejas kodu

fixed some memory leaks

Joshua Bicking 8 gadi atpakaļ
vecāks
revīzija
ff5d0fd89e
1 mainītis faili ar 4 papildinājumiem un 4 dzēšanām
  1. 4 4
      wordParse.c

+ 4 - 4
wordParse.c

@@ -12,8 +12,6 @@
 #include "wordParse.h"
 #include <ctype.h>
 
-#define MAX_WORD_SIZE 12
-
 char **wordArr;
 
 int numWords;
@@ -88,8 +86,8 @@ void readWordsFromFile(FILE* fp){
 
         wordArr = realloc(wordArr, sizeof(char*) * (numWords+1));
 
-        *(wordArr+numWords) = malloc(sizeof(char) * sizeof(buf));
-        strncpy(*(wordArr+numWords),buf, MAX_WORD_SIZE);
+        *(wordArr+numWords) = malloc(sizeof(char) * (strlen(buf)+1));
+        strcpy(*(wordArr+numWords),buf);
 
         numWords++;
     }
@@ -378,6 +376,8 @@ char * getCompleteProg() {
 }
 
 void freeAll() {
+    for(int i=0; i<numWords; i++)
+        free(*(wordArr+i));
     free(wordArr);
     free(victoryProg);
     free(completeProg);