error in C using malloc : corupted size vs prev_size -
i found answer python didn't understand it.
the code modified merge sort. working fine small number of inputs checked upto 10. when run through online judge, when number of inputs high (500) gave me error:
error in 'a.out': corrupted size vs. prev_size: 0x0000000000d5b8b0 ======= backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3b83a5b7e5] /lib/x86_64-linux-gnu/libc.so.6(+0x80dfb)[0x7f3b83a64dfb] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3b83a6853c] a.out[0x4009d1] a.out[0x400ac7] a.out[0x400a87] a.out[0x400aa4] a.out[0x400a87] a.out[0x400bc7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3b83a04830] a.out[0x4005b9] ======= memory map: ========
and goes 15 lines. why getting error? because of mistake making during dynamic allocation of memory using malloc
?
here's code:
#include <stdio.h> #include <stdlib.h> void *merge(int *a,int l,int m,int r,int *b,int *f); void *merge(int *a,int l,int m,int r,int *b,int *f){ int i=l,j=m,k=0,*c,x,y=l,z,csize,temp,*d,*e; csize = r-l; c = (int *) malloc (csize * sizeof(int)); d = (int *) malloc (csize * sizeof(int)); e = (int *) malloc (csize * sizeof(int)); while (k < csize){ if((j==r) || ((i!=m) && ((a[j]*b[i]) >= (a[i]*b[j])))){ c[k] = a[i]; d[k] = b[i]; e[k] = f[i]; i++; k++; } if((i>=m) || ((j!=r) && ((a[j]*b[i]) < (a[i]*b[j])))){ c[k] = a[j]; d[k] = b[j]; e[k] = f[j]; j++; k++; } } for(x=0;x<k;x++){ a[y] = c[x]; b[y] = d[x]; f[y] = e[x]; y++; } free(c); free(d); free(e); } void *mergesort(int *a,int left,int right,int *b,int *c); void *mergesort(int *a,int left,int right,int *b,int *c){ int mid,i,j,k=0,l=0,*r,*l; if(right - left == 1){ a[left] = a[left]; } if(right-left > 1){ mid = (left+right)/2; mergesort(a,left,mid,b,c); mergesort(a,mid,right,b,c); merge(a,left,mid,right,b,c); } } int main(){ int n,i=0,newnumt,newnumo,*a,*b,*c; scanf("%d",&n); = (int *) malloc (n * sizeof(int)); b = (int *) malloc (n * sizeof(int)); c = (int *) malloc (n * sizeof(int)); for(i=0;i<n;i++){ scanf("%d %d",&a[i],&b[i]); c[i]= i+1; } mergesort(a,0,n,b,c); for(i=0;i<n;i++){ printf("%d\n",c[i]); } return 0; }
Comments
Post a Comment