c - Linked List - program crashes while adding nodes -


my code compiles correctly, after 4 loops of insertlast function, program crashes. can me understand why?

i posted similar question earlier helped me identify other problems. i've rewritten function still have same problem. code below:

#include <stdio.h> #include <stdlib.h> #include "linkedlist.h"   int main (int argc, char* argv[])  {     int ii;          {         file* f; /*open file reading ints*/         f = fopen(argv[1], "r");          if(f==null)              {             printf("error: not open file");             return 0;             }      linkedlist* canqueue=createlist();      for(ii = 0; ii < 10; ii++)         {         tincan* tempcan= (tincan*) malloc(sizeof(tincan));         fscanf(f, " wgt_%d", &tempcan[ii].weight);         insertlast(canqueue, tempcan); /*inserts new can linked list*/         }     testlinkedlist(canqueue);     }     return 0;  } 

linkedlist.h

typedef struct tincan     {     int weight;     } tincan;  typedef struct node     {     tincan* data;     struct node *next;     } node;  typedef struct linkedlist     {     node *head;     } linkedlist;  void insertlast(linkedlist* list, tincan *newdata); linkedlist* createlist(); void testlinkedlist(linkedlist* list); 

linkedlist.c

#include <stdio.h> #include <stdlib.h> #include "linkedlist.h"  linkedlist* createlist() /*creates empty linked list*/   {     linkedlist* mylist;     mylist = (linkedlist*)malloc(sizeof(linkedlist));     mylist->head = null;     return mylist;   }  void insertlast(linkedlist* list, tincan *newdata)     {     node* newnode = (node*)malloc(sizeof(node));     newnode->data = newdata;     newnode->next = null;      if(list->head==null)         {         node* current = (node*)malloc(sizeof(node));         list->head=newnode;         current=newnode;         }          else             {             node* temp = (node*)malloc(sizeof(node));             temp = list->head;             while(temp->next!=null)                 {                 temp = temp->next;                 }              temp->next = newnode;             }   printf("looped\n");   }   void testlinkedlist(linkedlist* list)   {   node* current;   current = list->head;    while(current != null)     {     printf("weight = %d\n", current->data->weight);     current = current->next;     }   } 

these lines can removed:

node* current = (node*)malloc(sizeof(node)); current=newnode; 

this line doesn't need allocation of memory:

node* temp = (node*)malloc(sizeof(node)); 

i bet breaking on line though:

fscanf(f, " wgt_%d", &tempcan[ii].weight); 

tempcan isn't array, i'm not 100% sure &tempcan[ii] do, suspect you're accessing memory around tempcan pointer location , it's working 4 because that's size of something.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -