Help on coding, possible memory allocation error.

This is a discussion on Help on coding, possible memory allocation error. within the C Programming forums, part of the General Programming Boards category; I'm trying to understand linked list by writing a program but I seem to meet with wierd problems. Can anyone ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Help on coding, possible memory allocation error.

    I'm trying to understand linked list by writing a program but I seem to meet with wierd problems. Can anyone be helpful enough to help?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    typedef struct stringnode{
    	char *string; //pointer to string
    	struct stringnode *next; //points to next node
    } node;
    
    void printlist(node *);
    void printtable();
    void addnode(node **);
    
    void main(){
    	int input=1;
    
    	printf("\n\nWelcome to the Linked List Creation Program v1.0!!");
    	printf("\nThis program tests out the implementation of one linked list\n");
    
    	node *linklist=NULL; //creates a pointer of type node which points to NULL
    
    	while (input!=0){
    
    		printlist(linklist);
    		printtable();
    		scanf("%d", &input);
    
    		switch(input){
    			case 1:
    				addnode(&linklist);
    				break;
    			case 2:
    				break;
    			case 0:
    				printf("Program Terminating...");
    				break;
    			default:
    				printf("Command not recognized!");
    				break;
    		} // End of switch function
    
    	} // End of while
    
    	return;
    }
    
    ....
    
    void addnode(node **list){
    	int addPlace, x;
    	char word[101];
    	node *temp, *temp2;
    
    	temp = (node *)malloc(sizeof(node)); //flag one
    	temp = *list;
    
    	printlist(*list);
    	printf("\n\nPlease input the word you wish to add (enter any number to exit)");
    		//fgets(word, sizeof(word), stdin);
    		scanf("%s", &word);
    		if (isdigit(word[0])) return; //terminates function if data is numeric
    
    	printf("Where do you wish to add the new node after (0 to add at top)?");
    		scanf("%d", &addPlace);
    
    	printf("string:%s", word);
    	printf("\nnumber: %d", addPlace);
    
    	if(addPlace > 1 ){
    		for(x=1;x<addPlace;x++){
    			temp = temp->next;
    		}
    	} // end of if
    
    	if(*list == NULL){ //for first node only
    		temp2 = (node *)malloc(sizeof(node));
    		temp2->next = NULL;
    		strcpy(temp2->string, word);
    		*list = temp2;
    	} else {
    		temp2 = (node* )malloc(sizeof(node)); //flag two
    		temp2->next = temp->next; //flag three
    		strcpy(temp2->string, word);
    		temp->next = temp2;
    	}
    
    	//free(temp2);
    	printf("\nWord is added successfully...");
    
    	return;
    }
    To save space I ... the two functions which I believe have nothing to do with the problem. Within the main there is a loop which accesses the function addnode whenever I choose. My problem now is that when I access addnode for the second time after looping while once, my program terminates prematurely at the temp = (node *)malloc(sizeof(node)) which I commented //flag one on the code. If I comment that line, the program would terminate prematurely at the temp2 = (node*)malloc(sizeof(node)) which I commented //flag two. If I continue to comment that line, my program terminates prematurely at the place which I commented //flag three.

    Its really frustruating because I do not get any syntax errors with the compiler and I cannot debug. Can Anyone help me with it?

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Code:
    	temp = (node *)malloc(sizeof(node)); //flag one
    	temp = *list;
    Don't typecast the return of malloc. Also, soon as you define memory for temp, you assign it to point somewhere else, why? You might want to write out the logic on paper, such as make a pointer for the new node... Look at other people's code also, yours needs some work.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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