I am a beginner in c
i have written a linked list program and it shows a error after two successions please help me to find the error and to solveit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int userid;
struct node *next;
};
struct node *initialize()//to initialize node
{
(struct node *)malloc(sizeof (struct node*) );
}
struct node *append(struct node *headref,struct node *tailref)//to append new node
{
struct node *current;
struct node *newnode;
current=initialize();
newnode=initialize();
current=headref;
newnode->next=NULL;
printf("enter the value of new node\n");
scanf("%d",&newnode->userid);
if(current==NULL)
{
headref=newnode;
}
else
{
while(current->next!=NULL)
current=current->next;
current->next=newnode;
tailref=newnode;
}
free(newnode);
free(current);
}
struct node *read_list(struct node *headref)//to read the list of nodes
{
struct node *current;
current=initialize();
current=headref;
if(current!=NULL)
{
while(current->next!=NULL)
{
int c=1;
printf("the %d userdata is %d",c,current->userid);
c++;
current=current->next;
}
}
printf("the list is empty");
free(current);
}
main()
{
int choice=1;
struct node *head, *tail;
head=initialize();
tail=initialize();
while(choice!=0)
{
printf("enter your choice\n 1.for append\n 2.for readlist\n 0.for exit");
scanf("%d",&choice);
switch(choice)
{
case 1:
append(head,tail);
break;
case 2:
read_list(head);
break;
default:
printf("invalid option\n");
break;
}
}
free(head);
free(tail);
}
and the output is
enter your choice
1.for append
2.for readlist
0.for exitq
enter the value of new node
enter your choice
1.for append
2.for readlist
0.for exitenter the value of new node
*** glibc detected *** /home/sriram/myfolder/myfiles/cfiles/dynamic: double free or corruption (fasttop): 0x0a03a038 ***
(no debugging symbols found)
======= Backtrace: =========
/lib/libc.so.6[0xc05424]
/lib/libc.so.6(__libc_free+0x77)[0xc0595f]
/home/sriram/myfolder/myfiles/cfiles/dynamic[0x80484f4]
/home/sriram/myfolder/myfiles/cfiles/dynamic[0x80485ea]
/lib/libc.so.6(__libc_start_main+0xc6)[0xbb6de6]
/home/sriram/myfolder/myfiles/cfiles/dynamic[0x80483c9]
======= Memory map: ========
00205000-0020e000 r-xp 00000000 fd:00 3475750 /lib/libgcc_s-4.0.0-20050520.so.1
0020e000-0020f000 rwxp 00009000 fd:00 3475750 /lib/libgcc_s-4.0.0-20050520.so.1
00b80000-00b9a000 r-xp 00000000 fd:00 3475745 /lib/ld-2.3.5.so
00b9a000-00b9b000 r-xp 00019000 fd:00 3475745 /lib/ld-2.3.5.so
00b9b000-00b9c000 rwxp 0001a000 fd:00 3475745 /lib/ld-2.3.5.so
00ba2000-00cc6000 r-xp 00000000 fd:00 3475746 /lib/libc-2.3.5.so
00cc6000-00cc8000 r-xp 00124000 fd:00 3475746 /lib/libc-2.3.5.so
00cc8000-00cca000 rwxp 00126000 fd:00 3475746 /lib/libc-2.3.5.so
00cca000-00ccc000 rwxp 00cca000 00:00 0
00e11000-00e12000 r-xp 00e11000 00:00 0
08048000-08049000 r-xp 00000000 fd:00 4391253 /home/sriram/myfolder/myfiles/cfiles/dynamic
08049000-0804a000 rw-p 00000000 fd:00 4391253 /home/sriram/myfolder/myfiles/cfiles/dynamic
0a03a000-0a05b000 rw-p 0a03a000 00:00 0 [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0
b7d21000-b7e00000 ---p b7d21000 00:00 0
b7eeb000-b7eec000 rw-p b7eeb000 00:00 0
b7f0e000-b7f11000 rw-p b7f0e000 00:00 0
bf7fb000-bf811000 rw-p bf7fb000 00:00 0 [stack]
Program received signal SIGABRT, Aborted.
0x00e11402 in __kernel_vsyscall ()
SORRY for posting a long message but i have no other option......