C - dynamically sized array of struct pointers in another struct, segmentation fault -


i have segfault while trying run such code.

struct list; struct node;  typedef struct list {     struct node ** links;     int size;     int content; } list;  typedef struct node {      wchar_t value;     struct list* children;     int exists;  } node;   node* newnode(wchar_t value, int exists) {     node *q = (struct node*)malloc(sizeof(struct node));     q->value = value;     q->children = newlist();     q->exists = exists;     return q; }  list* newlist(){      list *result = (list*)malloc(sizeof(list));     result->size = 2;     result->content = 0;     result->links = (struct node**) malloc(result->size * sizeof(struct node*));      return result;  }  void resizelist(list* list_pointer){      if(list_pointer->size <= list_pointer->content){          list_pointer->size *= 2;         list_pointer->links = (struct node**) realloc(list_pointer->links, (list_pointer->size) * sizeof(struct node*));      } }  void pushlist(list* list_pointer, node* node_pointer){     if(node_pointer == null)         return;      resizelist(list_pointer);     list_pointer->content++;      int i;     node* temp_pointer;      for(i = 0; < list_pointer->content; i++){          if(list_pointer->links[i] == null){             list_pointer->links[i] = node_pointer;             break;         }          if(list_pointer->links[i]->value > node_pointer->value){             temp_pointer = list_pointer->links[i];             list_pointer->links[i] = node_pointer;             node_pointer = temp_pointer;         }     }  } 

calling.

struct list* l = newlist();  struct node* n1 = newnode(l'a', 1); struct node* n2 = newnode(l'b', 1); struct node* n3 = newnode(l'c', 1); struct node* n4 = newnode(l'd', 1); struct node* n5 = newnode(l'e', 1); struct node* n6 = newnode(l'f', 1); struct node* n7 = newnode(l'g', 1); struct node* n8 = newnode(l'h', 1);  pushlist(l, n1); pushlist(l, n2); pushlist(l, n3); pushlist(l, n4); pushlist(l, n5); pushlist(l, n6); pushlist(l, n7); pushlist(l, n8); 

after first 2 pushes fails.

it's supposed create list based on values stored in nodes. but... doesn't. throws segfault. when changed allocation of memory "sizeod(node*)" "sizeof(node)", works, presumably cause of allocation of bigger memory. want store pointers, not structs in array.

i'm figthing 6 hours no idea do.

if getting segfault, should include backtrace in question. make lot easier figure out what's going on.

looking @ code, see you're not clearing result->links when malloc or realloc it, relying on pointers being null inside pushlist. increase list_pointer->content , check if (list_pointer->links[i] == null). that's going result in undefined behavior sure.

memory not filled zeros when use malloc or realloc. if need case, need yourself. (you use calloc replacement malloc, doesn't realloc.)

this code fine if learning, though agree comment above it's bit of convoluted way of doing it. if production code should use open-source list library because debugged , tuned you.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -