Note that there are some explanatory texts on larger screens.

plurals
  1. POSingly Linked List struct insert/delete multiple free
    text
    copied!<p>Been looking at this code for too long and I am getting gloomy any chance of figuring it out by myself has been lost :( anyone can tell me where am I being stupid? I just don't understand where I am double freeing or possibly allocating incorrectly (which I must bee doing but yeah). I keep getting <strong>* glibc detected *</strong> free(): invalid next size</p> <p>Am I actually freeing more than I need to or am I just not allocating what I need to allocate in the first place. --sorry for bad indentation can't get this editor to indent correctly</p> <p>I have structures:</p> <pre><code>typedef int boolean; typedef char * String; typedef struct { char name[30]; long ID; char address[40]; char city[20]; int age; }Employee; typedef struct node { Employee *anEmployee; struct node *next; }NODE; typedef struct { NODE *head, *tail; }SLL; </code></pre> <p>insert function--SLL (Singly Linked List)</p> <pre><code>void insert(SLL *list, Employee e){ printf("insert"); NODE *temp, *current; temp = (NODE *)malloc(sizeof(NODE)); assert(temp != NULL); temp -&gt; anEmployee = (Employee *)malloc(sizeof(Employee *)); assert(temp -&gt; anEmployee != NULL); strcpy(temp -&gt; anEmployee -&gt; name, e.name); temp -&gt; anEmployee -&gt; ID = e.ID; strcpy(temp -&gt; anEmployee -&gt; address, e.address); strcpy(temp -&gt; anEmployee -&gt; city, e.city); temp -&gt; anEmployee -&gt; age = e.age; if (list -&gt; head == NULL) { /* list is empty */ list -&gt; head = list -&gt; tail = temp; return; } else { // list is not empty list -&gt; tail -&gt; next = temp; list -&gt; tail = temp; } } </code></pre> <p>deleting and freeing memory in delete function as such</p> <pre><code>boolean delete(SLL *list, String str){ printf("delete"); NODE *temp, *temp2; if (list -&gt; head == NULL) return FALSE; // list is empty temp = list -&gt; head; while ((temp != NULL) &amp;&amp; (strcmp(temp -&gt; anEmployee -&gt; name , str) != 0)) temp = temp -&gt; next; if (temp == NULL) return FALSE; // str is not found in the list // now temp points to the NODE with str in it. Let us delete it // from the list if ( list -&gt; head == temp) { // temp points to the first NODE if (temp -&gt; next == NULL) { // temp points to the only NODE list -&gt; head = list -&gt; tail = NULL; free(temp -&gt; anEmployee); free(temp); return TRUE; } // temp points to the first NODE but it is not the only NODE list -&gt; head = temp -&gt; next; free(temp -&gt; anEmployee); free(temp); return TRUE; } if (temp -&gt; next == NULL) { // temp points to the last NODE temp = list -&gt; head; temp2 = list -&gt; head -&gt; next; while(temp2 - &gt; next != NULL){ temp = temp-&gt;next; temp2 = temp2 -&gt;next; } list -&gt; tail = temp ; list -&gt; tail -&gt; next = NULL; free(temp2 -&gt; anEmployee); free(temp2); return TRUE; } // temp points to some NODE in the middle of the list temp2 = temp -&gt; next; while(temp2 - &gt; next != NULL){ temp -&gt;anEmployee = temp2 - &gt; anEmployee // temp = temp-&gt;next; temp2 = temp2 -&gt;next; } temp -&gt;anEmployee = temp2 - &gt; anEmployee list -&gt; tail = temp ; list -&gt; tail -&gt; next = NULL; free(temp2 -&gt; anEmployee); free(temp2); return TRUE; } </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