Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting a glibc error when adding using pointers
    text
    copied!<p>I have been getting the following error when I compile my program in terminal:</p> <pre><code>*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x089660a0 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0xb7621bc2] /lib/i386-linux-gnu/libc.so.6(+0x6f862)[0xb7622862] /lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0xb762594d] ./a.out[0x8048668] ./a.out[0x8048fa3] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75cc113] ./a.out[0x80484c1] ======= Memory map: ======== 08048000-0804a000 r-xp 00000000 08:06 1835029 /home/fasih/poly/a.out 0804a000-0804b000 r--p 00001000 08:06 1835029 /home/fasih/poly/a.out 0804b000-0804c000 rw-p 00002000 08:06 1835029 /home/fasih/poly/a.out 08966000-08987000 rw-p 00000000 00:00 0 [heap] b7400000-b7421000 rw-p 00000000 00:00 0 b7421000-b7500000 ---p 00000000 00:00 0 b757f000-b759b000 r-xp 00000000 08:06 4195258 /lib/i386-linux-gnu/libgcc_s.so.1 b759b000-b759c000 r--p 0001b000 08:06 4195258 /lib/i386-linux-gnu/libgcc_s.so.1 b759c000-b759d000 rw-p 0001c000 08:06 4195258 /lib/i386-linux-gnu/libgcc_s.so.1 b75b1000-b75b3000 rw-p 00000000 00:00 0 b75b3000-b7729000 r-xp 00000000 08:06 4195237 /lib/i386-linux-gnu/libc-2.13.so b7729000-b772b000 r--p 00176000 08:06 4195237 /lib/i386-linux-gnu/libc-2.13.so b772b000-b772c000 rw-p 00178000 08:06 4195237 /lib/i386-linux-gnu/libc-2.13.so b772c000-b772f000 rw-p 00000000 00:00 0 b772f000-b7757000 r-xp 00000000 08:06 4195267 /lib/i386-linux-gnu/libm-2.13.so b7757000-b7758000 r--p 00028000 08:06 4195267 /lib/i386-linux-gnu/libm-2.13.so b7758000-b7759000 rw-p 00029000 08:06 4195267 /lib/i386-linux-gnu/libm-2.13.so b776c000-b776f000 rw-p 00000000 00:00 0 b776f000-b7770000 r-xp 00000000 00:00 0 [vdso] b7770000-b778e000 r-xp 00000000 08:06 4195224 /lib/i386-linux-gnu/ld-2.13.so b778e000-b778f000 r--p 0001d000 08:06 4195224 /lib/i386-linux-gnu/ld-2.13.so b778f000-b7790000 rw-p 0001e000 08:06 4195224 /lib/i386-linux-gnu/ld-2.13.so bfed6000-bfef7000 rw-p 00000000 00:00 0 [stack] Aborted </code></pre> <p>This is my code, it appears to be happening in the polyAdd function, but I also get an assertion fail in multiply so it could be both?</p> <pre><code> #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; #include &lt;stdbool.h&gt; #include "poly.h" int polyDegree(struct poly *p) { return p-&gt;length-1; } struct poly *polyCreate() { struct poly *p = (struct poly *)malloc(sizeof(struct poly)); p-&gt;coeff=(double *)malloc(sizeof(double)); p-&gt;size=1; p-&gt;length=0; return p; } struct poly *polySetCoefficient (struct poly *p, int i, double value) { if(p == NULL) return; if(i&gt;=p-&gt;size) { do { p-&gt;size = p-&gt;size*2; }while(i&gt;=p-&gt;size); p-&gt;coeff = (double *)realloc(p-&gt;coeff, p-&gt;size*sizeof(double)); } while(i &gt;= p-&gt;length) { p-&gt;coeff[p-&gt;length] = 0; p-&gt;length++; } p-&gt;coeff[i] = value; return p; } struct poly *polyDelete(struct poly *p) { if (p){ free(p);} return 0; } struct poly *polyCopy(struct poly *p) { struct poly *nP = polyCreate(); nP-&gt;size =p-&gt;size; nP-&gt;length = p-&gt;length; int i = 0; for (i = 0; i&lt;(nP-&gt;size);i++) { nP-&gt;coeff[i] = p-&gt;coeff[i]; } return nP; } struct poly *polyAdd(struct poly *p0, struct poly *p1) { int i; struct poly *pF = polyCreate(); if (p0-&gt;length &gt; p1-&gt;length) { pF = polyCopy(p0); for (i=0;i&lt;p1-&gt;length;i++) pF-&gt;coeff[i] += p1-&gt;coeff[i]; } else if (p1-&gt;length &gt;= p0-&gt;length) { pF = polyCopy(p1); for (i=0;i&lt;p0-&gt;length;i++) pF-&gt;coeff[i] += p0-&gt;coeff[i]; } return pF; } struct poly *polyPrime (struct poly *p) { struct poly *pF = polyCreate(); pF-&gt;size = p-&gt;size; int i,j,k; int n = p-&gt;size; double a[n-1]; for (i = 1; i &lt;=n;i++) { a[i-1] = i * p-&gt;coeff[i]; } for (i = 0; i &lt; n; i++) { pF-&gt;coeff[i] = a[i]; } return pF; } struct poly *polyMultiply (struct poly *p0, struct poly *p1) { struct poly *product = polyCreate(); product-&gt;length = p0-&gt;length + p1-&gt;length - 1; product-&gt;size = p0-&gt;size + p1-&gt;size; product-&gt;coeff = (double *)malloc(product-&gt;size*sizeof(double)); int i,j; for(i=0;i&lt;product-&gt;length;i++) product-&gt;coeff[i] = 0; for(i=0;i&lt;p0-&gt;length;i++) for(j=0;j&lt;p1-&gt;length;j++) product-&gt;coeff[i+j] += p0-&gt;coeff[i] * p1-&gt;coeff[j]; return product; } double polyGetCoefficient(struct poly *p, int i) { double val =p-&gt;coeff[i]; return val; } int checkZero (double a[], int n) { int x = 0; for (x = 0; x &lt; n; x++) { if (a[x] != 0) return 1; } return 0; } double polyEval(struct poly *p, double x) { int i,n; double eval=0; if (!p) return 0; if (p) n = p-&gt;length; if (n == 0) return 0; for (i = 0; i&lt;=n;i++) { if (p-&gt;coeff[i] == 0)continue; if (i == 0)eval += p-&gt;coeff[0]; else eval += p-&gt;coeff[i]* pow (x,i); } return eval; } void polyPrint (struct poly *p) { int x=0,y,z; int n; n = p-&gt;size; double a[p-&gt;size]; for (x = p-&gt;size; x &gt;= 0; x--) { a[x] = p-&gt;coeff[x]; } bool check,neg,zero = true; if (!checkZero (a,n)) { printf("0\n"); } else{ for(x=(n-1);x&gt;=0;x--) { check = false; neg = false; if (x &lt; (n-1) &amp;&amp; a[x+1] == 0 &amp;&amp; a[x] != 0 &amp;&amp; a[x] &gt; 0 &amp;&amp; !zero) printf(" + "); else if (x &lt; (n-1) &amp;&amp; a[x+1] == 0 &amp;&amp; a[x] != 0 &amp;&amp; a[x] &lt; 0 &amp;&amp; !zero) { printf (" - "); a[x] = a[x] * -1.00; } if (a[x] == 0) continue; if (a[x] != 0.0&amp;&amp; x &gt; 1) { if (a[x] == -1)printf("-x^%d",x); else if (a[x] == 1)printf ("x^%d",x); else printf("%gx^%d",a[x],x); check = true;zero = false; } else if (x == 1) { if (a[x] == 1)printf("x"); else printf("%gx",a[x]); check = true; zero = false; } else if (x == 0) { printf("%g",a[x]); check = false; zero = false; } if (a[x-1] &lt; 0 &amp;&amp; x &gt; 0){ printf (" - ");a[x-1] = a[x-1] * -1.00;} else if (x &gt; 0&amp;&amp; a[x-1] != 0)printf(" + "); } printf("\n");} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload