473,322 Members | 1,403 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,322 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 7889
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.