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).