Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Freeing a malloc'd array at the wrong time?
    primarykey
    data
    text
    <p>I have a <code>struct cell</code> -></p> <pre><code>struct cell { double x, y, h, g, rhs; struct key *keys; }; </code></pre> <p>And I'm using the following method to free a cell -></p> <pre><code>void cellFree(struct cell *c) { // Free the keys free(c-&gt;keys); // Free the cell itself. free(c); } void cellFreeSors(struct cell *cn) { int i; for(i = 0; i &lt; 5; i++) { // Free keys free(cn[i].keys); } // Free array free(cn); } </code></pre> <p>Now, I'm facing a weird problem with one of the malloc'd arrays I've created. Basically, I'm trying to find the neighbors of a cell and doing some processing based on their values using the following two methods -></p> <pre><code>struct cell * cellGetSuccessors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) { int i; // CREATE 5 CELLS struct cell *cn = malloc(5 * sizeof (struct cell)); if (cn == NULL) { printf("--&gt; Unable to malloc *cn!\n"); errno = ENOMEM; return NULL; } for(i = 0; i &lt; 5; i++) { cn[i].keys = malloc(sizeof(struct key)); if (cn[i].keys == NULL) { printf("--&gt; Unable to malloc *cn[%d].keys!\n", i); errno = ENOMEM; return NULL; } cellCopyValues(&amp;cn[i], c); } // MAKE THEM NEIGHBORS // PROCESS return cn; } double cellRHS(struct cell *c, struct cell *sstart, struct cell *sgoal, double km, struct cell * prevCell) { // GET NEIGHBORS of c struct cell *cn = cellGetSuccessors(c, sstart, sgoal, km); double minsum; // SOME PROCESS TO UPDATE minsum minsum = 5.232111; // SAY // Free memory cellFreeSors(cn); return minsum; } </code></pre> <p>Trouble is, when I call <code>cellFreeSors()</code> in <code>cellRHS()</code>, I run into problems later.. This is how these functions are called.. </p> <pre><code>struct cell *u = cellCreateNew(); u-&gt;rhs = cellRHS(u, sstart, sgoal, km, prevCell); queueAdd(&amp;U, u); </code></pre> <p>This gives me a segmentation error when I try to print my queue -></p> <pre><code> QUEUE CONTENTS ================================================================== F -&gt; 0x2354550 L - &gt;0x2354550 (1) [0x2354550] X 50.000000, Y 45.000000 PREV: (nil) NEXT: 0x4014000000000000 Segmentation fault </code></pre> <p>As you can see, the NEXT for the entry seems to be initialized for some reason. The same code when executed WITHOUT <code>cellRHS()</code> works fine .. -></p> <pre><code>struct cell *u = cellCreateNew(); queueAdd(&amp;U, u); QUEUE CONTENTS ================================================================== F -&gt; 0x2354550 L - &gt;0x2354550 (1) [0x2354550] X 50.000000, Y 45.000000 PREV: (nil) NEXT: (nil) </code></pre> <p>Why is <code>cellFreeSors()</code> causing this problem? I've no use for the spawned neighbors of u beyond the scope of <code>cellRHS</code>. What am I doing wrong?</p> <p>Thanks..</p> <p>** EDITS The struct for queue_node is </p> <pre><code>/* QUEUE NODE * ---------------------------- * Contains a struct cell c and * reference to next queue_node */ struct queue_node { struct cell *c; struct queue_node *next; struct queue_node *prev; }; /* PRIORITY QUEUE * ---------------------------- * The queue itself, with first * and last pointers to queue_nodes */ struct priority_queue { struct queue_node *first; struct queue_node *last; }; </code></pre> <p>The <code>queuePrint()</code> method shows queue contents -></p> <pre><code>void queuePrint(struct priority_queue *q) { printf("\n\n\tQUEUE CONTENTS\n\t==================================================================\n"); int i = 1; struct queue_node *temp = q-&gt;first; printf("\tF -&gt; %p\n\tL -&gt; %p\n", q-&gt;first, q-&gt;last); while(temp != NULL) { printf("\t(%d) [%p]\tX %f, Y %f\tPREV: %p\tNEXT: %p", i, temp, temp-&gt;c-&gt;x, temp-&gt;c-&gt;y, temp-&gt;prev, temp-&gt;next); printf("\n"); temp = temp-&gt;next; i++; } printf("\n\n"); } </code></pre>
    singulars
    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.
    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