C Segmentation fault when adding hash node
I have a structure of type:
struct hashnode_s {
struct hashnode_s *next;
char *key;
ValueType tag;
union
{
int IntegerValue;
char *StringValue;
} u;
int IsInCycle;
};
And when I add an item of a type String, I have can, the code for it is
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 = ke开发者_运维技巧y;
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;
}
}
}
}
But when I add an item of the type integer. It throws a segmentation fault for some reason?
Here's that code.
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;
}
}
}
I'm trying to get rid of the segmentation fault, but I can't any ideas?
A few immediate issues spring to my attention when looking at your code:
- You always do a strcmp with StringValue even though it is in a union with IntegerValue, this means that strcmp will read an invalid memory address and cause a segfault for integer values.
- Is your hash-table initialized, if not then it's quite likely that the line of code you claim is segfaulting because it is the first time a write occurs on a page without write access.
- You check if the next node is NULL before assigning the allocated node to be the next node, if it fails then you don't free the memory, this memory leaking could be an issue. The obvious solution to number 1 and this would be to use the next pointer being NULL as an indicator that you've reached the end of the list rather than checking the string which may not always be there.
- The hash is a signed integer, ensure that the only negative number returned from the hashing function is -1 since any other negative could easily be another out-of-bounds access error.
If I were you then I'd fix these other problems before trying to track down this error, it's easier to find an error when you're only looking for one.
精彩评论