program hangs after execution

This is a discussion on program hangs after execution within the C Programming forums, part of the General Programming Boards category; This program will generate a list of temporary filenames using linked lists.I dont get an error when I compile, but ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    program hangs after execution

    This program will generate a list of temporary filenames using linked lists.I dont get an error when I compile, but when I execute it just hangs. This is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #ifdef NODEISSTRING
    #define NODEDATATYPE(var) char var [20]
    #else
    #define NODEDATATYPE(var) int var 
    #endif
    
    struct node {
    	NODEDATATYPE(data);
    	struct node	*next;
    };
    
    void insertlist(struct node **list, NODEDATATYPE(val)){
    
    	struct node *new, *cur, *back;
    	int	found;
    
    	new = malloc(sizeof(struct node));
    	if ( new == NULL){
    		fprintf(stderr, "Cannot allocate new.\n");
    		exit(1);
    	}
    
    /* pick correct comparison based on data type */
    #ifdef NODEISSTRING
            strcpy(new -> data, val);
    #else
            new -> data = val;
    #endif
    	back = NULL;
    	cur = *list;
    	found = 0;
    	while ( cur != NULL && !found ) 
    
    /* pick the right comparison based on whether we are a string */
    #ifdef NODEISSTRING
                    if ( strcmp(val, cur -> data) <= 0 )
    #else
                    if ( val <= cur -> data )
    #endif
    			found = 1;
    		else {
    			back = cur;
    			cur = cur -> next;
    		}
    	new -> next = cur;
    	if ( back == NULL ) /* must be first element */
    		*list = new;
    	else
    		back->next = new;
    }
    
    void deletelist(struct node **list) {
    
    	struct node *n;
    
    	n = (*list) -> next;
    
    	if ( n == NULL )
    		(*list) -> next = NULL;
    	else
    		/* copy whole struct */
    		**list = *n;
    
    	free(n);
    }
    
    struct node *searchlist(struct node *list, NODEDATATYPE(val)) {
    
    	struct node *cur;
    	int found;
    
    	cur = list;
    	found = 0;
    
    	while ( cur != NULL && !found ) {
    
    #ifdef NODEISSTRING
    		if ( strcmp(cur -> data, val ) == 0 )
    #else
    		if ( cur -> data == val )
    #endif
    			found = 1;
    		else
    			cur = cur -> next;
    	}
    
    	return cur;
    
    }
    
    int emptylist(struct node *list){
    	return (list == NULL);
    }
    
    void dumplist(struct node *list) {
    	struct node *p;
    
    	p = list;
    	while ( p != NULL ) {
    
    #ifdef NODEISSTRING
    		printf("%s\n", p -> data);
    #else
    		printf("%d\n", p -> data);
    #endif
    		p = p -> next;
    	}
    }
    
    void clearlist(struct node **list) {
    	struct node *p, *c;
    
    	p = *list;
    	while ( p != NULL ) {
    		c = p;
    		p = p -> next;
    		free(c);
    	}
    	*list = NULL;
    }
    
    int main () {
    
    	struct node *list, *p;
    	FILE *out;
    	int x;
    
    	out = fopen("d6.out", "w");			/* Opening file d6.out for writing */
     	
     	if(out == NULL) {						/* File test for out */
              perror("fopen:     d6.out");
              exit(1);
         }
         
         for(x = 0; x < 15000; x++)
              insertlist(&list, tmpnam(NULL));
              
         p = list;
    	
    	while ( p != NULL ) {
    
    		fprintf(out, "%s\n", p -> data);
    		fprintf(out, "%d\n", p -> data);
    		p = p -> next;
    	}
    	
    	clearlist(&list);
    	fclose(out);
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > insertlist(&list, tmpnam(NULL));
    How about
    list = NULL;
    before you append to a junk memory location
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Have to use tmpnam.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Erm, I said nothing about tmpnam, I talked about list.
    Like
    struct node *list, *p;
    is UNINITIALISED
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Did what you said and no luck.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My guess is that this is at least part of your problem.
    Code:
    void deletelist(struct node **list) {
    
    	struct node *n;
    
    	n = (*list) -> next;
    
    	if ( n == NULL )
    		(*list) -> next = NULL;
    	else
    		/* copy whole struct */
    		**list = *n;
    
    	free(n);
    }
    1) This will crash on an empty list.
    2) What is this function even trying to do?

    Let's examine with actual words instead of code:

    1) Assign whatever is at list->next to N.
    2) If N is null, then make list->next NULL. Um... why? If it's already null, what's the point of assigning it null?
    3) If it's not null, copy whatever is there over top of the current node. Again, why? Why don't you just update the top of the list pointer, and delete the old top?
    4) Now free N, which we've just copied over top of the top of the list's data, and free not the top, which we should be freeing, but N which is AFTER the top.

    That's just ... ugly is a nice way to put it. Let's try it this way, shall we?
    Code:
    void delete( struct node **list )
    {
        struct *n = *list;
    
        if( n )
        {
            *list = (*list)->next;
            free( n );
        }
    }
    See how much better that is? Now of course, the only way that could fail is if you actually pass the function NULL, instead of a valid pointer. But I'll leave that grueling fix as an exercise to the reader.

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

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Briefly, without looking at the code have you thought of using a debugger and stepping through your code. This will show you where it is hanging.

    Just a thought.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execution of a C program
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 11:14 AM
  2. invoke the execution of one c program from another
    By kris.c in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-08-2006, 11:10 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 05:00 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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