c++ - Structure having an array of pointers in c -


i implementing multibit trie in c. getting segmentation fault error. when run program. don't know going wrong?

the node of multi bit trie that:

struct mtnode{         /* nodes array 8 elements. each element pointer child node.*/         mtnode* nodes[8];  // 2^stride = 2^3 = 8         int   nexthop;     }; 

each node initialized following:

typedef mtnode node; node *init_mtnode(){     node *ret = (node*) malloc(sizeof(node));        int size = (int)pow(2,stride);     (int i=0; i<size ; ++i)     {            ret->nodes[i] = null;     }         ret->nexthop = -1;     return ret; } 

is wrong in init_mtnode method?

there multiple possible reasons:

  1. you don't check malloc() null, unlike new throw exception, malloc() return null if error happens.

  2. you calculate value of size , loop size times, array can hold 8 pointers.

try this

typedef mtnode node; node *init_mtnode() {     node *ret;     int size;     ret = static_cast<node *>(malloc(sizeof(node)));     if (ret == null) /* check null */         return null;     size = 2 << stride;     if (size >= 8)         size = 7; /* maximum possible value */     (int = 0 ; < size ; ++i)         ret->nodes[i] = null;     ret->nexthop = -1;      return ret; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -