Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault when malloc/free appear in loop in C
    primarykey
    data
    text
    <p>I have a program that basically looks like:</p> <pre><code> typedef struct cpl_def { int A; int B; int OK; struct cpls *link; }cpls; int main(void) { int n1, n2; int num = 300; /* say */ int *a; a = NULL; int *apt; int *b; b = NULL; int *bpt; cpls *cplptr; cplptr = NULL; int i, j; for (i=0; i &lt; 2000; i++) { if (i == 0) { cplptr = (cpls *) malloc(num*sizeof(cpls) ); /* create the structure */ initalize(cplptr); } /* ...operations on cplptr ... */ FOO(cplptr); /* ...determine I need a subset of size n1 (a positive integer of size n1 which changes during the loop) entries from cplptr ... */ n1 = FOO2(cplptr); n2 = FOO3(cplptr); /* ...figure out the values of A, B for additional n2 entries into cplptr ... */ cplptr2 = (cpls *) malloc(n2*sizeof(cpls) ); /* a second structure to store additional entries */ /* .... operations on cplptr2 ...*/ /* ...copy subset of n1 entries from cplptr into dynamically allocated arrays a,b of size n1... */ a = malloc(n1 * sizeof(int)); apt = &amp;a[0]; b = malloc(n1 * sizeof(int)); bpt = &amp;b[0]; for (j=0; j &lt; num; j++) { if (cplptr[j].OK==1) { (*apt++) = cplptr[j].a; (*bpt++) = cplptr[j].b; } } free(cplptr); /* free the first structure */ cplptr = (cpls *) malloc((n1+n2)*sizeof(cpls) ); /* redeclare the first structure to reflect the proper sizes */ for (j = 0; j &lt; n1; j++) /* transfer a subset of size n1 to the first structure */ { cplptr[j].a = a[j]; cplptr[j].b = b[j]; cplptr[j].OK = 1; } for (j = n1; j &lt; n1 + n2; j++) /* transfer things to the first structure */ { cplptr[j].a = cplptr2[j].a; cplptr[j].b = cplptr2[j].b; cplptr[j].OK = cplptr2[j].OK; } free(a) free(b) free(cplptr2); /* free the second structure */ } /* End iteration i } /* End main() */ </code></pre> <p>This is just the skeletal form but it hopefully provides enough of a picture. ANyhow it generally runs fine, but for some values of n1, n2, the free(cplptr) seems to cause a segmentation fault. It's only called once, and I check the address after the malloc() call to cplptr and before the corresponding free() for cplptr.</p> <pre><code>.... cplptr = (cpls *) malloc(num*sizeof(cpls) ); printf("fine to this %p\n", &amp;cplptr[0]); ... printf("fine to this %p\n", &amp;cplptr[0]); free(cplptr) &lt;- segmentation fault happens here. </code></pre> <p>The addresses match, meaning free() should be freeing what it's supposed to, right?? gdb gives Program received signal SIGSEGV, Segmentation fault. 0xb7ce179b in ?? () from /lib/tls/i686/cmov/libc.so.6 and step Cannot find bounds of current function</p> <p>Is there another way to implement something like that avoids the segmentation faults?</p> <p>Thanks a million for your suggestions! Any idea what's going on??</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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