我有一个类型的结构:
struct hashnode_s {
struct hashnode_s *next;
char *key;
ValueType tag;
union
{
int IntegerValue;
char *StringValue;
} u;
int IsInCycle;
};
当我添加一个String类型的项目时,我可以,它的代码是
int hashtbl_InsertString(HASHTBL *hashtbl, const char *key, const char *value)
{
struct hashnode_s *node;
hash_size hash;
hash = SearchForHashIndex(hashtbl, key,value);
if(hash == -1)
{
hash=hashtbl->hashfunc(key);
}
/* adding the first node if not applicable (this is based on value string)*/
if(hashtbl->nodes[hash]== NULL)
{
node = malloc(sizeof(struct hashnode_s));
node->key = key;
node->tag = StringConst;
node->u.StringValue = value;
node->next = NULL;
hashtbl->nodes[hash] = node;
}
else
{
node = hashtbl->nodes[hash];
if(node->next ==NULL)
{
struct hashnode_s *nextNode;
nextNode = malloc(sizeof(struct hashnode_s));
if(strcmp(node->u.StringValue,key)==0)
{
/* set next */
nextNode->key = key;
nextNode->tag = StringConst;
nextNode->u.StringValue = value;
nextNode->next = NULL;
node->next = nextNode;
hashtbl->nodes[hash] = node;
}
else if(strcmp(node->key, value)==0)
{
/* switch node positions if the key */
nextNode->key = key;
nextNode->tag = StringConst;
nextNode->u.StringValue = value;
node->next = NULL;
nextNode->next = node;
node = nextNode;
hashtbl->nodes[hash] = nextNode;
}
}
else
{
while(node)
{
struct hashnode_s *nextNode;
nextNode = malloc(sizeof(struct hashnode_s));
/* TESTING PURPOSES ONLY
printf("#define %s %s\n",node->key,node->u.StringValue);
printf("%s==%s\n",node->u.StringValue,key);
printf("%s==%s\n\n\n",node->key, value);
*/
if(strcmp(node->u.StringValue,key)==0)
{
nextNode->key = key;
nextNode->tag = StringConst;
nextNode->u.StringValue = value;
nextNode->next = NULL;
node->next = nextNode;
return 0;
}
else if(strcmp(node->key, value)==0)
{
nextNode->key = key;
nextNode->tag = StringConst;
nextNode->u.StringValue = value;
node->next = NULL;
nextNode->next = node;
node = nextNode;
return 0;
}
node = node->next;
}
}
}
}
但是当我添加一个整数类型的项目时。它出于某种原因抛出了分段错误?
这是代码。
int hashtbl_InsertValue(HASHTBL *hashtbl, const char *key, int integerValue)
{
struct hashnode_s *node;
hash_size hash;
hash = SearchByKey(hashtbl, key);
if(hash == -1)
{
hash=hashtbl->hashfunc(key);
}
if(hashtbl->nodes[hash] ==NULL)
{
node = malloc(sizeof(struct hashnode_s));
node->key = key;
node->tag = IntegerConst;
node->u.IntegerValue = integerValue;
node->next = NULL;
hashtbl->nodes[hash] = node;
return 0;
}
else
{
node = hashtbl->nodes[hash];
//Check(hashtbl);
while(node)
{
if(strcmp(node->u.StringValue,key)==0)
{
struct hashnode_s *nextNode;
nextNode = malloc(sizeof(struct hashnode_s));
nextNode->key = key;
nextNode->tag = IntegerConst;
nextNode->u.IntegerValue = 5;
nextNode->next = NULL;
if(node->next == NULL)
{
// THIS IS WHERE IT CRASHES AT!
node->next = nextNode;
}
return 0;
}
node=node->next;
}
}
}
我正试图摆脱分段错误,但我不能有任何想法?
答案 0 :(得分:0)
在查看代码时,我会注意到一些紧急问题:
如果我是你,那么在尝试追踪这个错误之前我会修复这些其他问题,当你只找一个错误时更容易找到错误。