Thread: stuck on small bug

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    stuck on small bug

    I have a program I'm working on that will read a txt file and count the unique words and how many times that word appears.

    I'm getting an error when I try to run it that says "Debug Error! Run-Time Check Failure #3 - The variable 'last' is being used without being initialized."

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/stat.h>
    
    /*
    	Custom list data type
    */
    typedef struct list_s {
    	struct list_s *prev, *next;
    	char *text;
    	int count;
    } list_t;
    
    /*
    	Add text to the list.
    	Returns the list on sucess.
    	Returns NULL on addition failures,
    		i.e. the text already there or no memory available.
    */
    static list_t *list_add(list_t *list, char *text)
    {
    	list_t *last, *node, *n;
    
    	for (n = list; n != NULL; n = n->next) {
    		if (!stricmp(n->text, text)) {
    			n->count++;
    			return NULL;
    		}
    		last = n;
    	}
    
    	if (!(node = malloc(sizeof(list_t)))) {
    		return NULL;
    	}
    	node->text = strdup(text);
    	node->count = 1;
    	node->next = NULL;
    
    	if (last == NULL) {
    		node->prev = NULL;
    		return node;
    	}
    
    	node->prev = last;
    	last->next = node;
    
    	return list;
    }
    
    /*
    	free the whole list and its associated data
    */
    static void list_free(list_t *list)
    {
    	list_t *node = list;
    
    	while (node != NULL) {
    		list_t *next = node->next;
    		if (node->text)
    			free(node->text);
    		free(node);
    		node = next;
    	}
    }
    
    /*
    	simple print of word list
    */
    static void list_print(list_t *list)
    {
    	list_t *node;
    	for (node = list; node; node = node->next)
    		printf("%s %d\n", node->text, node->count);
    }
    
    /*
    	simple file print of word list
    */
    static void list_fprintf(FILE *fp, list_t *list)
    {
    	list_t *node;
    	for (node = list; node; node = node->next)
    		fprintf(fp, "%s %d\n", node->text, node->count);
    }
    
    
    list_t *word_list = NULL; /* our word list */
    
    
    int main(int argc, char **argv)
    {
    	struct stat buff;
    	int size;
    	FILE *file = NULL;
    	unsigned char *data = NULL;
    	char *ptr, *text = NULL;
    	list_t *list = NULL;
    
    	/* check arguments for input */
    	if (argc < 2) {
    		printf("Usage: %s <INPUT> [OUTPUT]\n", argv[0]);
    		return -1;
    	}
    
    	/* get input file information */
    	if (stat(argv[1], &buff)) {
    		printf("Error: can't open file \"%s\"\n", argv[1]);
    		return -1;
    	}
    	size = buff.st_size;
    	if (size < 1) {
    		printf("Info: empty file\n");
    		return 0;
    	}
    
    	/* allocate memory to load the file */
    	if (!(data = malloc(size + 1))) {
    		printf("Error: not enough memory\n");
    		return -1;
    	}
    
    	/* now load the file to memory */
    	if (!(file = fopen(argv[1], "rb"))) {
    		printf("Error: can't open file \"%s\"\n", argv[1]);
    		return -1;
    	}
    	fread(data, size, 1, file);
    	data[size] = 0;
    	fclose(file);
    
    	/* reassign to standard output */
    	file = stdout;
    
    	/* now parse the data */
    	for (ptr = data; ptr - data < size; ) {
    		int c = *ptr;
    		if (isalnum(c)) {
    			if (!text) /* word starts */
    				text = ptr;
    		} else {
    			if (text) { /* word ends */
    				c = *ptr;
    				*ptr = 0;
    
    				/* add word to the list */
    				if (list = list_add(word_list, text))
    					word_list = list;
    
    				*ptr = c;
    
    				text = NULL; /* reset state */
    			}
    		}
    		ptr++;
    	}
    
    	/* free memory */
    	free(data);
    
    	/* optionally opens output file */
    	if (argc > 2) {
    		if (!(file = fopen(argv[2], "w"))) {
    			printf("Info: can't open \"%s\"\n", argv[2]);
    			file = stdout;
    		}
    	}
    
    	/* now output the list */
    	/*list_print(word_list); */
    
    	list_fprintf(file, word_list);
    
    	/* optionally close the file */
    	if (file && file != stdout)
    		fclose(file);
    
    	/* free the list */
    	list_free(word_list);
    
    	return 0; /* done */
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That's because there is a path through list_add() where you don't do
    last = someValidPointer;

    before you try
    if ( last == NULL )

    As error messages go, it's pretty damn obvious what it means.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    I apologize for posting something so simple. I have only been messing with C for a couple months and have only done a couple simple programs with it.

    Anyways I fixed the error by adding this

    last = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck....
    By Hulkbuster in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2002, 09:58 PM
  2. stuck!!!!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2002, 01:21 AM
  3. help me please, im stuck
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 04-19-2002, 06:36 PM
  4. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM
  5. Still stuck, please help
    By Gladers in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2001, 10:33 PM