Я прохожу онлайн-курс МООК по информатике в Гарварде (CS 50), а для одного из наборов задач вам нужно загрузить словарь в какую-то структуру данных (я выбрал Trie), а затем использовать указанную структуру данных для проверки информации. Я нахожусь в том месте, где пишу свою функцию, которая загружает словарь, и мне представилась серьезная проблема. Моя попытка, несмотря на попытки обнаружить указатели NULL, продолжает выделять память.
Вот мой код:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"
#include <ctype.h>
//Defines the trie data structure which is used to store the words
struct trieNode{
char letter;
bool wordComp;
bool isRoot;
struct trieNode* childNodes[27];
}trie;
//The trie that will be used for the rest of the program
struct trieNode root;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// TODO
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
//Counts to see whether the word was just an \n or it was really a word
int cLettCount = 0;
//counts the number of total words used
int tWordCount = 0;
//basic setup stuff
root.isRoot = true;
//Pointer to the current node (current node starts as root node).
struct trieNode* currNode = &root;
FILE* dic = fopen(dictionary, "r");
char currChar = 1;
//main loop (Goes till end of file)
while(currChar != EOF){
//resets letter count
cLettCount = 0;
//resets currNode to the root node
currNode = &root;
currChar = 1;
//Loop gathers current word and pushes the letters to the trie
for(int j = 0; currChar != '\n'; j++){
currChar = fgetc(dic);
//Makes sure we're not at the end of a word or at EOF
if(currChar == '\n'|| currChar == EOF)
{
break;
}
//Makes sure we only get alpha chars
else if(!isalpha(currChar) && currChar != 39)
{
printf("Nonalpha char %c detected. Quiting...\n", currChar);
return false;
}
//Meat and bones. Adds letters onto the trie
else
{
bool isNull = false;
if(currChar == 39){
currNode = currNode->childNodes[26];
if(currNode->childNodes[26] == NULL)
{
isNull = true;
}
}
else
{
//Finds the node that corrosponds with the letter
if(currNode->childNodes[currChar - 97] == NULL)
{
isNull = true;
}
currNode = currNode->childNodes[currChar - 97];
}
if(isNull)
{
currNode = malloc (sizeof (trie));
currNode->isRoot = false;
currNode->letter = currChar;
currNode-> wordComp = false;
printf("Node created for: %c\n", currNode->letter);
}else if(!isNull){
printf("Node not created\n");
}
}
}
if(currChar == EOF){
break;
}
else if(currChar == '\n'){
currNode -> wordComp = true;
tWordCount++;
printf("\n");
}
}
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
// TODO
return 0;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// TODO
return false;
}
Load()
вот где моя проблема.