Why I got such weird results for a simple linked list test?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phe2003
    New Member
    • Feb 2008
    • 2

    Why I got such weird results for a simple linked list test?

    Hi All, I've been testing some extremely simple code about a linked list.

    What I keep doing is actually writing some parts of a linked list and testing them.

    Below are my codes:
    ---------------------------------------------------------------------------

    /* new.h */
    #ifndef NEW_H
    #define NEW_H

    typedef int ListEntry;

    typedef struct listnode{
    ListEntry info;
    struct listnode *next;
    }ListNode;

    typedef struct list{
    ListNode *head;
    ListNode *tail;
    int count;
    }List;

    void CreatList(List *list);
    int ListSize(List *list);

    #endif

    -------------------------------------------------------
    /* new.c */

    #include <stdio.h>
    #include <stdlib.h>
    #include "new.h"

    void CreatList(List *list){
    list = (List*)malloc(s izeof(List));
    if(list==NULL) Error("No memory available.");
    list->head = list->tail = NULL;
    list->count = 0;
    }

    int ListSize(List *list){
    return (list->count);
    }



    --------------------------------------------
    /* newtest.c */

    #include <stdio.h>
    #include <stdlib.h>
    #include "new.h"

    main(){
    List *list;
    CreatList(list) ;
    printf("%d\n", ListSize(list)) ;
    }

    --------------------------------------------------------



    I have 3 files: .h, .c and test.c
    I try to test if I successfully created a new linked list
    If I did, I should have got a result ListSize(list) == 0;
    But I didn't...below are the compiling and running results:

    With one system, I got:

    [phe@f438-07 hw4]$ #include "new.h"
    [phe@f438-07 hw4]$ gccx newtest.c new.o
    [phe@f438-07 hw4]$ gccx newtest.c new.c
    [phe@f438-07 hw4]$ ./a.out
    -2130706312


    With another system, I got:

    [phe@inceptor mergesort]$ #include "new.h"
    [phe@inceptor mergesort]$ gccx newtest.c new.o
    [phe@inceptor mergesort]$ gccx newtest.c new.c
    [phe@inceptor mergesort]$ ./a.out
    33949697

    Then, what's wrong...??? I am really frustrated, this is rather simple code and test, but I got so wired errors...Should n't the ListSize(list) be zero when I just created a list?

    Or, anything wrong with memory?

    Really appreciate your help!!!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by phe2003
    [code=c]
    typedef struct list{
    ListNode *head;
    ListNode *tail;
    int count;
    }List;
    [/code]
    You never initialize count. So when the compiling process starts, the memory location for count is created, but the value inside is left alone. This means it contains whatever values were in there before you ran your program, or is a "junk" variable. This is why you need to set variables on instantiation.

    Comment

    • phe2003
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by sicarie
      You never initialize count. So when the compiling process starts, the memory location for count is created, but the value inside is left alone. This means it contains whatever values were in there before you ran your program, or is a "junk" variable. This is why you need to set variables on instantiation.
      Thanks for your reply. But I did initialize list->count in new.c...


      void CreatList(List *list){
      list = (List*)malloc(s izeof(List));
      if(list==NULL) Error("No memory available.");
      list->head = list->tail = NULL;
      list->count = 0;
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by phe2003
        main(){
        List *list;
        CreatList(list) ;
        printf("%d\n", ListSize(list)) ;
        }
        Your problem is here: CreateList() takes a List* argument:
        Originally posted by phe2003
        void CreatList(List *list){
        list = (List*)malloc(s izeof(List));
        if(list==NULL) Error("No memory available.");
        list->head = list->tail = NULL;
        list->count = 0;
        }
        So the call in main() takes the pointer list and makes a copy of it for the CreateList() arguiment. The function dutifully creates the list and updates the copy. The original pointer in main() never gets changed.

        You need to change CreateList() to takes the address of a List*. That way CreateList() can change the address in the LIst* in main():

        [code=cpp]
        void CreatList(List **list){
        *list = (List*)malloc(s izeof(List));
        etc...
        }

        int main()
        {
        List *list;
        CreatList(&list );

        }
        [/code]

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Dang, thanks WFC, good catch. That's what I get for spot reading it without code tags.

          Comment

          Working...