c - Linked List Data All The Same -


i'm trying read in input .txt file, , add singly linkedlist. problem i'm having nodes being created , connected correctly (i'm getting correct length), after adding node, each data field same. wondering problem is, , how fix it. been going @ awhile!

add function:

#include "linkedlist.h" #include <stdlib.h> #include <string.h>  void addordered(node ** head,char * input) {          printf("\ninput: %s\n",input);          node * cur = (*head);          node * newnode = malloc(sizeof(node));         newnode->data = malloc(sizeof(person));          newnode->data->fname = strtok(input," ");         newnode->data->lname = "test";         newnode->data->id = 5;         newnode->next = null;          if(cur == null)         {             (*head) = newnode;         }         else         {              for(;cur->next != null;cur = cur->next)             {                 printf(" cur->next ");             }              cur->next = newnode;         } } 

processing file:

void processfile(node ** head, file * fd) {     char * input = malloc(sizeof(char)*size);      while(fgets(input,size,fd) != null)     {         addordered(head,input);     }       free(input); } 

the bug in code is: address assign newnode->data->fname becomes invalid once call free(input) in processfile() function , access free memory causes 'undefined behavior' @ run time.

to correct code:

instead of simple assignment like:

newnode->data->fname = strtok(input," "); 

allocate , copy in separate memory below:

char *ptr =  strtok(input," "); newnode->data->fname = malloc(strlen(ptr)+1); strcpy(newnode->data->fname, ptr); 

actually assigning memory address input address space in addordered() , makes free() in processfile() function.

i explain code elaborate way below:

first read: char * strtok ( char * str, const char * delimiters ); manual:

on first call, function expects c string argument str, first character used starting location scan tokens. in subsequent calls, function expects null pointer , uses position right after end of last token new starting location scanning.

return value

a pointer last token found in string. null pointer returned if there no tokens left retrieve.

it doesn't sends new memory memory input lately free(). understand strtok() function work written following code:

int main (){   char str[] ="- this, sample string.";   printf("str address: %p, ends on:  %p \n", str, str+strlen(str));   printf ("splitting string \"%s\" tokens:\n",str);   char * pch;   pch = strtok (str," ,.-");   while (pch != null){     printf ("pch: %7s,  address: %p\n",pch, pch);     pch = strtok (null, " ,.-");   }   return 0; } 

an execution of above program (address can different @ each run):

~$ ./a.out  str address: 0x7fff96958d50, ends on:  0x7fff96958d68  splitting string "- this, sample string." tokens: pch:    this,  address: 0x7fff96958d52 pch:       a,  address: 0x7fff96958d58 pch:  sample,  address: 0x7fff96958d5a pch:  string,  address: 0x7fff96958d61 

notice: pch address from/within str address scape.

similarly in code, assign newnode->data->fname = strtok(input," "); in addordered() function. memory address value @ newnode->data->fname in/from input latterly free in processfile() function newnode->data->fname become invalid , code runs undefined behavior

void addordered(node ** head,char * input){          printf("\ninput: %s\n",input);         // code here                                                      step-2          newnode->data->fname = strtok(input," "); <--"assign memory"                                                   ^       //  code here                        | }                                               |                                                    |   void processfile(node ** head, file * fd){      |    step-1     char * input = malloc(sizeof(char)*size); <-|----"allocate memory"     while(fgets(input,size,fd) != null){        |         addordered(head,input); -----------------     }                                                step-3       free(input); <-----------------------------------"free memory" }  so, "what assign newnode->data->fname becomes invalid" 

second, should read 1 less char file in buffer, keep space null \0.

fgets(input, size-1, fd) 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -