Thread: getting weird Esception on freeing link list

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

    getting weird Esception on freeing link list

    Following is the source:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <ctype.h>
    
    
    typedef struct
    {
    	int val;
    	char *path;
    	struct items *next;
    }items;
    
    
    
    items *head=NULL;
    
    void insert(items *temp,int val,char *path)
    {
    	
    //I am only trying to free this memory
    char *pathval = malloc(strlen(path));
    	items *prev = head;
    	
    	strcpy(pathval,path);
    
    	temp = malloc(sizeof(items));
    
    	if(temp == NULL)
    	{
    		assert(0);
    		return;	
    	}
    	
    	if(head == NULL)
    	{
    		temp->val = val;
    		temp->next = NULL;
    		temp->path = pathval;
    		head = temp;		
    	
    	}
    	else
    	{
    		while(prev->next != NULL)
    			prev=prev->next;
    
    		temp->val = val;
    		temp->next = NULL;
    		temp->path = pathval;
    		prev->next = temp;		
    
    	}
    
    }
    
    
    void Display(items *DispNd)
    {
    
    	if(DispNd == NULL)
    		printf("Link List is empty!!!!\n");
    
    	printf("Values in LinkList Nodes:\n");
    	while(DispNd)
    	{
    		printf("%d\t",DispNd->val);
    		printf("%s\t",DispNd->path);
    		DispNd = DispNd->next;
    	}
    	
    }
    
    void vFree(items *temp)
    {
    	while(head)
    	{
    		temp=head->next;
    		free(head->path);  //error 
    		free(head);
    		head=temp;
    	}
    }
    
    int main()
    {
    	int i,len;
    	char *path = "John is a good boy";
    	
    	printf("Enter the length of linked list:");
    	scanf("%d",&len);
    
    	for(i=0;i<len;i++)
    	{
    		insert(head,i,path);
    	
    	}
    
    	Display(head);	
    	vFree(head);
    	//printf("Val of :");
    	return 0;
    }

    Exception received:

    DAMAGE:after Normal block


    What is wrong in the above highlighted line I am only trying free memory allocated (highlighted in blue).

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The size of a string is "strlen(string) + 1".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by GReaper View Post
    The size of a string is "strlen(string) + 1".
    Thanks.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help freeing a linked list
    By Muster Mark in forum C Programming
    Replies: 2
    Last Post: 06-14-2011, 05:24 PM
  2. Freeing a linked list -- possible bug.
    By msh in forum C Programming
    Replies: 5
    Last Post: 01-25-2011, 12:22 PM
  3. Freeing a linked list
    By Matty_Alan in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2010, 11:19 PM
  4. Weird Output...(Link list)
    By davidvoyage200 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2003, 09:07 PM
  5. freeing list
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 06:45 PM