Thread: Exiting the program.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Exiting the program.

    Can anyone help me with the below code?

    I have got it running, but after starting the program it hangs after outputting 1,2,3,4.... instead of outputting then automatically exiting.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Small program to explore using singly linked ring lists.
     
       A ring list is one in which the last item in the list points
       first item in the list rathern than NULL
    */
    
    struct intRecord
    {
       int number;
       struct intRecord *next;
    };
    
    typedef struct intRecord intNode;
    
    void copyArrayToList(intNode **start, int array[], int size);
    void printList(intNode *start);
    void freeList(intNode **start);
    intNode* newNode(int number);
    
    int main()
    {
       int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
       intNode *start = NULL;
    
       copyArrayToList(&start, array, 10);
       printList(start);
       freeList(&start);
    
       return 0;
    }
    
    void copyArrayToList(intNode **start, int array[], int size)
    {
       /* Create a new linked ring list and copy the contents of 
          array into it
       */
    
       int i;
       intNode *temp;
    
       for (i=0; i<size; i++)
       {
          temp = newNode(array[i]);
          if (*start == NULL)
          {
             /* add 1st item to ring list */
             *start = temp->next = temp;
          }
          else
          {
             /* add another item to ring list */
             temp->next = (*start)->next;
             (*start)->next = temp;
             *start = temp;
          }
       }
    
       /* make start point to the 1st item added to the ring list */
       if (*start != NULL) *start = (*start)->next;
    }
    
    void printList(intNode *start)
    {
       /* Print the contents of the ring list,
          starting with the first item added. 
       */
       intNode *bookmark = start;
       
       while (start != NULL)
       {
          printf("%d ", start->number);
          start = start->next;
          if (start == bookmark) break;
       }
       printf("\n");
    }
    
    void freeList(intNode **start)
    {
       /* Free up memory allocated to linked ring list */
    
       intNode *temp;
    
       /* check that ring list is not empty */
       if (*start == NULL) return;
    
       /* remove all items, until one remaining in ring list */
       while (*start != (*start)->next)
       {
          temp = (*start)->next;
          (*start) = temp->next;
          free(temp);
       }
    
       /* free up last remaining item in ring list */
       free(*start);
       *start = NULL;
    }
    
    intNode* newNode(int number)
    {
       /* dynamicaly allocate memory for an intNode
          make sure it is initialised correctly
       */
    
       intNode *temp;
    
       temp = (intNode *) malloc(sizeof(intNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
       temp->number = number;
       temp->next = NULL;
    
       return temp;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    I doubt some logical problem in function freeList().
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I am quite doubtful that freeList does what you expect it to. If you run through the procedure on paper with a typical ring list, you'll see what I mean.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Running using ./a.out I get:

    1 2 3 4 5 6 7 8 9 10


    Which is the output I am meant to get.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    And what does freeList do on that ring list ? That's what you should do on paper.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    intNode* newNode(int number)
    {
       /* dynamicaly allocate memory for an intNode
          make sure it is initialised correctly
       */
    
       intNode *temp;
    
       temp = (intNode *) malloc(sizeof(intNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
    1. You shouldn't cast malloc(), at least in some peoples' opinions. See the FAQ.
    2. Consider using sizeof(*temp) rather than hard-coding the type of temp into the malloc() call.
    3. "dynamically"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Thumbs up

    I hope u hav got the solution.. if not then...

    Problem was at /* remove all items, until one remaining in ring list */
    According to your logic u are trying to free the memory which is already free. It was only a hang in ur program but sometimes doing so could be disastrous.

    Code:
    while (*start != (*start)->next)
       {
          temp = (*start)->next;
          (*start) = temp->next; // Logical mistake
          free(temp);
       }
    
    in this change to
    (*start)->next = temp->next;
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exiting program
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 04-01-2006, 03:27 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  4. Exiting a program
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-14-2004, 08:51 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM