Thread: Linked List Not Saving Value as Int

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Linked List Not Saving Value as Int

    I've looked through my code alot and can not seem to find the error, i really appreciate your help. The problem is that when i call cord[] (part of node) in the main file it is not returning an int. I have put a check in the main file (isdigit()) to attempt to check if the returned value is an int. cord[] is an int array and values are being saved as ints so i'm not sure why it is not an int when it is called later on in main. I've included all the files that are needed for the program to compile and edit.

    Any suggestions will be greatly appreciated.

    node .h
    Code:
    #include <stdio.h>
    
    #ifndef _NODE_H
    #define _NODE_H
    
    /* Struct to handle the linked list itself */
    typedef struct node_tag{
      	char surface[30];
      	int cord[8];
      	struct node_tag	*next;
      	struct node_tag	*prev;
    } Node;
    
    typedef struct linked_list{
    	Node *head;
    	Node *tail;
    } list;
    
    
    Node *make_node(list *shapes, char *surface, int cord[]);
    void push_back(list *shapes, char *surface, int cord[]);
    void release(list *shapes) ;
    void walk_list(list *shapes) ;
    void delete_node(list *shapes, Node *p) ;
    
    #endif
    level.c (main)
    Code:
    #include "node.h"
    
    list *getLevel(int x);
    
    int main(void){
    	//Create list for level
    	list *level = NULL;
    	level = (list *)malloc(sizeof(list));
    
    	//get the current level - Takes a int parameter (the level you want to load) and returns a linked list.
    	level = getLevel(1);
            Node *p = level->head;
    	if(isdigit(p->cord[0])){ 	
    	printf("%d", p->cord[0]);
    	}
    
    	//DEBUG - Print the contents of the Level Linked List
    	walk_list(level);
    	return 0;
    }
    
    list *getLevel(int x){
    	
    	list *shapes = NULL;
    	shapes = (list *)malloc(sizeof(list));
    	
    	char extension[5] = ".txt";
    	char surface[30];
    	char level[30];
    	int cord[8];
    	FILE *cfPtr;
    
    	//Concatenate int level (x) and char extension into file name
    	sprintf(level, "%d%s", x, extension);
    
    	//check to see if file exists
    	if((cfPtr = fopen(level, "r")) == NULL){
    		printf("File could not be opened\n");
    	}
    	else{
    		//scan the first line
    		fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, &cord[0], &cord[1], &cord[2], &cord[3], &cord[4], &cord[5], &cord[6], &cord[7]);
    		
    		//Loop until you reach the end of the file
    		while(!feof(cfPtr)){
    			//add surface to linked list
    			push_back(shapes, surface, cord);
    			//scan for next surface
    			fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, &cord[0], &cord[1], &cord[2], &cord[3], &cord[4], &cord[5], &cord[6], &cord[7]);
    		} 
    
    		//close the file
    		fclose(cfPtr);
    
    	}
    
    	return shapes;
    
    }
    list.c
    Code:
    #include "node.h"
    
    Node *make_node(list *shapes, char *surface, int cord[]){
    	Node *tmp = NULL;
    	int x = 0;
    
      	tmp = (Node *)malloc(sizeof(Node)) ;
     	if (tmp == NULL) {
    		fprintf(stderr, "Error allocating memory in make_node().\n") ;
        		exit(1) ;
    	}
    
    	//add surface to node
      	if (surface)
         		strncpy(tmp->surface, surface, sizeof(tmp->surface)) ;
    	
    	//add each coordinate to node
    	for(x=0; x<8; x++){
    		tmp->cord[x] = cord[x];
    	}
    
      	tmp->next = NULL ;
      	tmp->prev = NULL ;
    
      	return tmp ;  
    
    }
    
    void push_back(list *shapes, char *surface, int cord[]){
     	Node	*tmp = NULL ;
    
      	if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
         		fprintf(stderr, "Inconsistent list in call to push_front().\n") ;
         		exit(2) ;
      	}
       
      	tmp = make_node(shapes, surface, cord);
      	if (!tmp) {
        		fprintf(stderr, "Error building node in push_front().\n") ;
        		exit(3) ;
      	}
    
      	if (shapes->head == NULL && shapes->tail == NULL) {
        		shapes->head = tmp ;
        		shapes->tail = tmp ;
      	} else {
       		shapes->tail->next = tmp ;
        		tmp->prev = shapes->tail ;
        		shapes->tail = tmp ;
      	}
    
      	return ;
    }
    
    void release(list *shapes) {
    
      	while (shapes->head)
        		delete_node(shapes, shapes->tail) ;
    
      	return ;
    } 
    
    void walk_list(list *shapes) {
      	Node	*p = shapes->head ;
      	int   count = 0 ;
    
      	if (shapes->head == NULL && shapes->tail == NULL) {
         		printf("======================\nEmpty List.\n=====================\n") ;
         		return ;						/* not an error to try to print an empty list */
     	}
    
      	if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
         		fprintf(stderr, "Error!  Inconsistent list in call to walk_list()\n") ;
         		exit(4) ;
      	}
    
      	while (p) {
      		/*  printf("Node[%d]:%s", count, p->surface);*/
        		printf("Node[%d]:  %s, %d, %d, %d, %d, %d, %d, %d, %d\n", count, p->surface, p->cord[0], p->cord[1], p->cord[2], p->cord[3], p->cord[4], p->cord[5], p->cord[6], p->cord[7]) ;
        		count++ ;
        		p = p->next ;
      	}
    
    
    }
    
    void delete_node(list *shapes, Node *p) {
    
      	if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
         		fprintf(stderr, "Inconsistent list in call to delete_node\n") ;
         		exit(5) ;
      	}
    
      	if ((shapes->head == NULL && shapes->tail == NULL) || (p == NULL))
         		return ;
    
      	if (p == shapes->head && p == shapes->tail) { /* Only one item in the list */
         		shapes->head = NULL ;
         		shapes->tail = NULL ;
      	} else if (p == shapes->head) {       /* If we delete the head node */
      		shapes->head = shapes->head->next ;
         		shapes->head->prev = NULL ;        /* only valid because of previous line */
      	} else if (p == shapes->tail) {       /* If we delete the tail node */
         		shapes->tail = shapes->tail->prev ;
         		shapes->tail->next = NULL ;
      	} else {                      /* If we delete a node in the middle */
         		Node *pre_p, *post_p ;
    
         		pre_p  = p->prev ;
         		post_p = p->next ;
    
         		pre_p->next = post_p ;
         		post_p->prev = pre_p ;
      	}
    
      	if (p)
         		free(p) ;
    
      	return ;
    
    }
    1.txt
    start 36 156 72 156 72 120 36 120
    end 0 534 36 534 36 498 0 498
    surface 0 120 1024 120 1024 0 0 0
    surface 215 210 416 210 416 192 215 192
    surface 468 282 668 282 668 264 468 264
    surface 720 354 920 354 920 336 720 336
    surface 567 426 342 426 342 408 567 408
    surface 0 498 514 498 514 480 0 480

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    isdigit() checks characters, not integers.

    isdigit('0') (the character), returns true
    isdigit(0) (the integer) returns false

    If you want to know if you read it in properly, check the fscanf() result.
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int main(void){
        //Create list for level
        list *level = NULL;
        level = (list *)malloc(sizeof(list));
    
        //get the current level - Takes a int parameter (the level you want to load) and returns a linked list.
        level = getLevel(1);
    
        ...
    }
    
    list *getLevel(int x){
    	
        list *shapes = NULL;
        shapes = (list *)malloc(sizeof(list));
    	
        ...
    
        return shapes;
    
    }
    You are throwing away the address of the list member allocated in main and overwriting it with a newly allocated block when you call the getLevel function (memory leak). Also you shouldn't cast malloc calls in C (which by the way you should be #including the stdlib.h header for).
    Last edited by hk_mp5kpdw; 05-04-2009 at 11:17 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    My understanding of c memory usage is not the best at the moment. So how should i go about fixing the memory leak. I need to section off memory in both getLevel and main to set the variable type and save the linked list to it don't I?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In the first section, you're allocating memory, and pointing at it.
    In the second, you're allocating more memory, and making the pointer point to the new memory.
    You need to free up the old memory, before you stop pointing at it. Or, you need to actually make use of your first block of memory, and not bother using the new chunk.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM