我有一个表,我正在实现数据结构.我的代码的一个重要块看起来像这样(h是数据结构,表是存储其他结构的表):
(h->table[hash(key, h->size)]
Run Code Online (Sandbox Code Playgroud)
当我的代码运行时,一些table [i]将被初始化,而有些则不会.每当我在尚未初始化的表[i]上测试我的代码时,我得到一个valgrind错误,基本上我说我无法使用未初始化的数组.
我的问题是,我如何检查某个h-> table [i]是否已初始化?
我正在制作一个程序来解析一行中的单词,当一个单词遇到一个非字母数字字符时,它会向树中添加一个单词.一行中没有空格,一切都很顺利.但是,当存在非字母数字字符时,有问题的循环(从代码中注释的行开始)的大小减半!
为什么循环减半?
Tree addin (char* filee, Tree tree)
{
int i;
FILE *fp;
fp = fopen(filee, "r");
char* hold2 = malloc(99);
int count=-1;
char* hold;
while ((hold=getLine(fp))!=NULL)
{
count=-1;
for (i=0; i<strlen(hold); i++) //The loop in question
{
count++;
if ((isalnum(hold[count])==0)&&(hold[count]!='\n'))
{
strncpy(hold2, hold, count);
hold2[count]='\0';
hold=strdup(&hold[count+1]);
count=-1;
tree = insertT(tree, hold2);
}
}
tree = insertT(tree, hold);
}
free(hold);
fclose(fp);
return tree;
}
Run Code Online (Sandbox Code Playgroud)