Transferring text from file into linked list

This is a discussion on Transferring text from file into linked list within the C Programming forums, part of the General Programming Boards category; if readdata returns pointer allocated by malloc/calloc - yes, you can free it in the main function. But you should ...

  1. #16
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if readdata returns pointer allocated by malloc/calloc - yes, you can free it in the main function.
    But you should check that this pointer is not null before using it.
    And better set to NULL after freeing to avoid acidential using of the already freed memory.

    In this small sample it is not important, but can be cretical in the big project.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    ok, im quite a bit furthur with this project but I have another problem *surprise*.

    I have got my program to read the list in now, but I am trying to search it and it seems to start the list at the last word entered and then tries to access the one after it rather than the word entered before it.

    I have included the code I have used below although there is quite a lot.

    I get a segmentation fault in the searchlist function when trying to access the next item of the list. I think this is a fairly stupid mistake so hopefully something will jump out.

    Thanks

    Code:
    char searchlist(list *root, char *word, int a, int b){
         
         if (root->thisnode[0] == '&'){
            return a;
         }
         else if(strcmp(word, root->thisnode) == 0){   
            a++;
            b++;
            searchlist(root->next, word, a, b);
         }
         else{
            a++;
            searchlist(root->next, word, a, b);
         }
         return 0;
    }
    
    list *insertlinkedlist(char *x){
         list *a;
         char y[50];
         int b;
         int c;
         int e;
         
         b = 0;
         e = 0;
         
         while (x[b] != '\0'){
               c = 0;
               
               while (x[b] != ' '){
                     y[c] = x[b];
                     b++;
                     c++;
               }     
               y[c] = '\0';
    
               a = insertlist(y, a);
               b++;
         }
         y[0] = '\0';
         a = insertlist(y,a);
         return a;
    }
    
    list *insertlist(char hd[50], list *a1){
         list *a;
         int b;
         int c;
         
         b = strlen(hd);
         a = calloc(1,sizeof(list));
         
         for (c = 0; c < b; c++){
             a->thisnode[c] = hd[c];
         }
         
         a->next = a1;
         return a;
         free (a);
    }
    
    int main(){
        char a[30];
        char b[50];
        char *y;
        list *z;
        int c;
        
        scanf("%s", a);
        y = readdata(a);
        printf("%s", y);
        z = insertlinkedlist(y);
        
        printf("\nEnter word for retrieval:");
        scanf("%s", b);
        c = searchlist(z,b,0,0);
        printf("%d", c);
        
        free(y);
        free(z);
        return 0;
    }

  3. #18
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what I see in your searchlist function

    in the main you call it as searchlist(z,b,0,0)
    Code:
    char searchlist(list *root, char *word, int a, int b){
         
         if (root->thisnode[0] == '&'){
            return a;//that is 0
         }
         else 
    //do some stuff but always got to the bottom line
        return 0;
    }
    so your search always returns 0; Right?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    well a is incremented in the program which then calls itself, so it shouldn't be 0 when the search gets to &, and the program would not compile without the return 0

    also is it possible to return two values, as I need a and b to be returned to the main program or will I need to insert them both into an array.

    the problem line is:
    [code]
    searchlist(root->next, word, a, b);
    [\code]

    as root->next seems to be set to an invalid memory address.

    thanks for your help

  5. #20
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by manutdfan
    well a is incremented in the program which then calls itself,
    yes, but return values of the subsequent calls are ignored and function goes directly to the return 0; statement
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    when I debug it, it seg faults when it tries to re-call itself as the root->next address is invalid.

    although I have changed the return now so it returns a instead of 0 but I have the same problem.

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by manutdfan
    when I debug it, it seg faults when it tries to re-call itself as the root->next address is invalid.

    although I have changed the return now so it returns a instead of 0 but I have the same problem.
    it indicates the problem in your insert function

    for example first call to insertlist function is made with uninitialized var a, so you set the next pointer in the first entered node to the ramdom value...

    It seems to me that you also have some problems in your 2 loops (what will happen if the inside loop come to the 0 char?)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    how would I solve this problem? because the way I have stored it isn't the last thing stored using the insertlist function the head of the list. As all the pieces of data are linked to the one added before it.

    As it starts at last one entered shouldn't it seg fault when it reaches the first item inserted in the list?

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Insert at the end instead of the beginning. Pretty simple.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    I was messing about with the search function to see if there was anything wrong in it, and I found that the root->next is connected together correctly.

    I used the following code to test whether hello would actually change to the next word in the list when it passes through the first time and it does. The memory address match up and all, so I can't really understand why it seg faults as I thought this only happens when trying to access a memory location that is unavaliable (which the first one isn't, even if the last one it would test would be).

    Code:
    char searchlist(list *root, char *word, int a, int b){
         list *hello;
         hello = root->next;
         if (root->thisnode[0] == '&'){
            return a;
         }
         else if(strcmp(word, root->thisnode) == 0){   
            a++;
            b++;
            return searchlist(hello, word, a, b);
         }
         else{
            a++;
            return searchlist(hello, word, a, b);
         }
         return a;
    }

  11. #26
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
         list *a;
    //some code here
               a = insertlist(y, a);
    a is not initialized when you call insertlist
    with your lack of accuraness in the code - you should initialize all the vars you use, even if you don't think it is needed.

    pointers should be initialized to NULL, other vars to some artifitial values.

    when you use pointers - you should check that each pointer you dereference is not null
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #27
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    so this would be solved be inserting the calloc statement before the function is called rather than in the function?

  13. #28
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by manutdfan
    so this would be solved be inserting the calloc statement before the function is called rather than in the function?
    So this would be solved by
    list* a = NULL;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #29
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    i've changed that now but I still get the same problem, it has me baffled as the root->next node points to the correct memory address but it seg faults.

  15. #30
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    before you dereference the pointer - check that it is not null
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21