C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-04-2009, 09:48 AM   #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
Quote:
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
bar338 is offline   Reply With Quote
Old 05-04-2009, 09:59 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-04-2009, 11:14 AM   #3
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
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).
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Last edited by hk_mp5kpdw; 05-04-2009 at 11:17 AM.
hk_mp5kpdw is offline   Reply With Quote
Old 05-04-2009, 07:39 PM   #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?
bar338 is offline   Reply With Quote
Old 05-04-2009, 07:53 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED HELP READING FILE and PRINTING geoffr0 C Programming 4 04-16-2009 05:26 PM
Sorting linked list please help with CODE scarlet00014 C Programming 3 09-27-2008 11:24 PM
memory leak aruna1 C++ Programming 3 08-17-2008 10:28 PM
Function argument assignment between types "unsigned int*" and "unsigned long*" nadeer78 C Programming 8 03-10-2008 11:57 AM
My graphics library stupid_mutt C Programming 3 11-26-2001 06:05 PM


All times are GMT -6. The time now is 08:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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