C Segmentation fault when adding hash node -


i have structure of type:

struct hashnode_s {     struct hashnode_s *next;     char *key;     valuetype tag;     union     {         int integervalue;         char *stringvalue;     } u;     int isincycle; }; 

and when add item of type string, have can, code 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 first node  if not applicable (this 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 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                 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;             }         }     } } 

but when add item of type integer. throws segmentation fault reason?

here's 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)                 {                     // crashes at!                     node->next = nextnode;                 }                  return 0;             }             node=node->next;         }     } } 

i'm trying rid of segmentation fault, can't ideas?

a few immediate issues spring attention when looking @ code:

  1. you strcmp stringvalue though in union integervalue, means strcmp read invalid memory address , cause segfault integer values.
  2. is hash-table initialized, if not it's quite line of code claim segfaulting because first time write occurs on page without write access.
  3. you check if next node null before assigning allocated node next node, if fails don't free memory, memory leaking issue. obvious solution number 1 , use next pointer being null indicator you've reached end of list rather checking string may not there.
  4. the hash signed integer, ensure negative number returned hashing function -1 since other negative out-of-bounds access error.

if i'd fix these other problems before trying track down error, it's easier find error when you're looking one.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -