473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

*** glibc detected *** ./a.out: corrupted double-linked list: 0x0000000000a5e240 ***

Hi i am Deepika. am new to this forum. I am working on ubuntu 10.04 LTS and using C lang. to write prgrms.
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
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<malloc.h>
  4. int main()
  5. {
  6. char **a, **b, str1[10],str2[10],s[80];
  7. int i=0,nl=0;
  8. FILE *fp;
  9.  
  10. fp=fopen("abc.c","r"); 
  11.  
  12. while(fgets(s,79,fp)!=NULL)               //Counts the no. of lines in a file
  13.        nl++;
  14. printf("\n The no. of lines are : %d ",nl); 
  15.  
  16. rewind(fp);
  17. a=(char **)malloc(nl);
  18. if(a==NULL)
  19.   {
  20.     printf("\n Not enough memory ");
  21.     return 0;
  22.   }
  23. b=(char **)malloc(nl);
  24. if(b==NULL)
  25.   {
  26.     printf("\n Not enough memory ");
  27.     return 0;
  28.   }
  29.  
  30. for(i=0;i<nl;i++)
  31.   {
  32.     fscanf(fp,"%s%s",str1,str2);
  33.  
  34.     a[i]=(char *)malloc(strlen(str1)+1);
  35.     b[i]=(char *)malloc(strlen(str2)+1);
  36.     strcpy(a[i],str1);
  37.     strcpy(b[i],str2);
  38.  
  39.   }
  40.  
  41. fclose(fp);
  42.  
  43. printf("\n");
  44. for(i=0;i<nl;i++)
  45.  {
  46.    printf("\n%s\t\t%s",a[i],b[i]);
  47.  }
  48.  
  49. for(i=0;i<nl;i++)
  50.  {
  51.     free(a[i]);
  52.     free(b[i]);
  53.  }
  54. free(a);
  55. free(b);         
  56.  
  57. printf("\n");
  58. return 0;
  59. }
After execution the output displays :

*** 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
Jul 4 '10 #1
2 7904
weaknessforcats
9,208 Expert Mod 8TB
I expect the heap is trashed and you crash making the calls to free() to clean up your mallocs.

I would use my debugger and carefully record the mallocs and verify all of the strcpy fits in those allocation.

Also be sure you don't free the same memory twice.
Jul 4 '10 #2
I post the answer, compiling in Debian GNU/Linux 8 testing, and also in Debian 7 stable, both:

When you do a malloc, you have to take in consideration all the array, in case of char**:

(char **)malloc(nl*sizeof(char *));

Because is an array of char*. nl is the size of the array, and sizeof(char *) means that each position have an char *.

In the case of an array of char, you have to malloc like this;

(char *)malloc((strlen(str1)+1)*sizeof(char));

(strlen(str1)+1) is the size of the array, and sizeof(char) means that each position have an char.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.     #include<string.h>
  3.     #include<malloc.h>
  4.     int main()
  5.     {
  6.     char **a, **b, str1[10],str2[10],s[80];
  7.     int i=0,nl=0;
  8.     FILE *fp;
  9.  
  10.     fp=fopen("abc.c","r"); 
  11.  
  12.     while(fgets(s,79,fp)!=NULL)               //Counts the no. of lines in a file
  13.            nl++;
  14.     printf("\n The no. of lines are : %d ",nl); 
  15.  
  16.     rewind(fp);
  17.     a=(char **)malloc(nl*sizeof(char *));//(char **)malloc(nl);
  18.     if(a==NULL)
  19.       {
  20.         printf("\n Not enough memory ");
  21.         return 0;
  22.       }
  23.     b=(char **)malloc(nl*sizeof(char *));
  24.     if(b==NULL)
  25.       {
  26.         printf("\n Not enough memory ");
  27.         return 0;
  28.       }
  29.  
  30.     for(i=0;i<nl;i++)
  31.       {
  32.         fscanf(fp,"%s%s",str1,str2);
  33.  
  34.         a[i]=(char *)malloc((strlen(str1)+1)*sizeof(char));
  35.         b[i]=(char *)malloc((strlen(str2)+1)*sizeof(char));
  36.         strcpy(a[i],str1);
  37.         strcpy(b[i],str2);
  38.  
  39.       }
  40.  
  41.     fclose(fp);
  42.  
  43.     printf("\n");
  44.     for(i=0;i<nl;i++)
  45.      {
  46.        printf("\n%s\t\t%s",a[i],b[i]);
  47.      }
  48.  
  49.     for(i=0;i<nl;i++)
  50.      {
  51.         free(a[i]);
  52.         free(b[i]);
  53.      }
  54.     free(a);
  55.     free(b);
  56.  
  57.     printf("\nSaliendo\n");
  58.     return 0;
  59.     }
Apr 19 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Vincent Zhu | last post by:
I moved ASP WebPages from Windows 2000 server to Windows 2003 server platform. Those double-byte characters (Japanese) retrieved from SQL server 2000 are corrupted. I am using Macromedia...
3
by: Paminu | last post by:
I get this error after running my application for some time. What does it mean and what should I be looking for in my code?
1
by: mahiapkum | last post by:
hello all i have a code which looks fine when reviewed but when the application has a long run say for example of 2 days it gets exit, because of glibc error and the sample code is as follows: while...
36
by: gert | last post by:
Any comments why char **page doesn't reallocate #include <stdlib.h> void add(char **page,char *line,int n) { char **temp; if(temp=realloc(page,sizeof(char *)*(n+1))) {
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
4
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
3
by: Renzr | last post by:
I have a C++ package which works very well in the 32-bit Linux-like OS. However, it will lead to a "*** glibc detected *** ./ex2: munmap_chunk(): invalid pointer" in 64-bit (Fedora 7-64), when it...
1
by: shiznit | last post by:
I made a program that finds a minimum spanning tree. In my main I created kruskal k; and called k.findMST(); void kruskal :: findMST(){
7
Gulzor
by: Gulzor | last post by:
Hi, I fetch web pages using Zend_Http (I send out POST data, fetch the results, and so on) I have no problem with that. I did a mb_detect_encoding() of the returned HTML and the function says...
1
by: itxharsh | last post by:
/* While running the program a runtime error occurs."glibc detected" followed by a lot of memory address/instruction errors. Please help rectify this error.*/ /*Header file:"nonpreemptive.h" is as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.