I am trying to read strings from a file where i am using malloc concept to declare and initialize string arrays. After execution i get the above mentioned error. I don't know where i am going wrong and i don't know how to solve the problem. Help would be greatly appreciated. My program code(C language) is as follows :
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<string.h>
- #include<malloc.h>
- int main()
- {
- char **a, **b, str1[10],str2[10],s[80];
- int i=0,nl=0;
- FILE *fp;
- fp=fopen("abc.c","r");
- while(fgets(s,79,fp)!=NULL) //Counts the no. of lines in a file
- nl++;
- printf("\n The no. of lines are : %d ",nl);
- rewind(fp);
- a=(char **)malloc(nl);
- if(a==NULL)
- {
- printf("\n Not enough memory ");
- return 0;
- }
- b=(char **)malloc(nl);
- if(b==NULL)
- {
- printf("\n Not enough memory ");
- return 0;
- }
- for(i=0;i<nl;i++)
- {
- fscanf(fp,"%s%s",str1,str2);
- a[i]=(char *)malloc(strlen(str1)+1);
- b[i]=(char *)malloc(strlen(str2)+1);
- strcpy(a[i],str1);
- strcpy(b[i],str2);
- }
- fclose(fp);
- printf("\n");
- for(i=0;i<nl;i++)
- {
- printf("\n%s\t\t%s",a[i],b[i]);
- }
- for(i=0;i<nl;i++)
- {
- free(a[i]);
- free(b[i]);
- }
- free(a);
- free(b);
- printf("\n");
- return 0;
- }
*** glibc detected *** ./a.out: corrupted double-linked list: 0x000000000187d240 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7ff28bc005b6]
/lib/libc.so.6(+0x7aa25)[0x7ff28bc03a25]
/lib/libc.so.6(cfree+0x73)[0x7ff28bc06e53]
/lib/libc.so.6(fclose+0x14d)[0x7ff28bbf0abd]
./a.out[0x400ad2]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7ff28bba7c4d]
./a.out[0x4007f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:08 123 /home/deepika/prg/a.out
00600000-00601000 r--p 00000000 08:08 123 /home/deepika/prg/a.out
00601000-00602000 rw-p 00001000 08:08 123 /home/deepika/prg/a.out
0187d000-0189e000 rw-p 00000000 00:00 0 [heap]
7ff284000000-7ff284021000 rw-p 00000000 00:00 0
7ff284021000-7ff288000000 ---p 00000000 00:00 0
7ff28b972000-7ff28b988000 r-xp 00000000 08:07 21469 /lib/libgcc_s.so.1
7ff28b988000-7ff28bb87000 ---p 00016000 08:07 21469 /lib/libgcc_s.so.1
7ff28bb87000-7ff28bb88000 r--p 00015000 08:07 21469 /lib/libgcc_s.so.1
7ff28bb88000-7ff28bb89000 rw-p 00016000 08:07 21469 /lib/libgcc_s.so.1
7ff28bb89000-7ff28bd01000 r-xp 00000000 08:07 19153 /lib/libc-2.11.1.so
7ff28bd01000-7ff28bf01000 ---p 00178000 08:07 19153 /lib/libc-2.11.1.so
7ff28bf01000-7ff28bf05000 r--p 00178000 08:07 19153 /lib/libc-2.11.1.so
7ff28bf05000-7ff28bf06000 rw-p 0017c000 08:07 19153 /lib/libc-2.11.1.so
7ff28bf06000-7ff28bf0b000 rw-p 00000000 00:00 0
7ff28bf0b000-7ff28bf2b000 r-xp 00000000 08:07 6347 /lib/ld-2.11.1.so
7ff28c10b000-7ff28c10e000 rw-p 00000000 00:00 0
7ff28c127000-7ff28c128000 rw-p 00000000 00:00 0
7ff28c129000-7ff28c12b000 rw-p 00000000 00:00 0
7ff28c12b000-7ff28c12c000 r--p 00020000 08:07 6347 /lib/ld-2.11.1.so
7ff28c12c000-7ff28c12d000 rw-p 00021000 08:07 6347 /lib/ld-2.11.1.so
7ff28c12d000-7ff28c12e000 rw-p 00000000 00:00 0
7fffe73e5000-7fffe73fa000 rw-p 00000000 00:00 0 [stack]
7fffe73ff000-7fffe7400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
The no. of lines are : 9 Aborted