Note that there are some explanatory texts on larger screens.

plurals
  1. POFreeing memory allocated by strdup
    primarykey
    data
    text
    <p>How would I free the memory allocated by strdup? I've tried using free(linepos) at the end of this function, but that causes my program to fail. I don't have linepos outside of this function, so I'm unable to free it anywhere else. Thank you in advance.</p> <pre><code>void putinqueue (queue *the_queue, FILE *input, char *filename) { char buffer[1024]; for (int linenr = 1; ; ++linenr) { char *linepos = fgets (buffer, sizeof buffer, input); if (linepos == NULL) break; linepos = strchr (buffer, '\n'); if (linepos == NULL) { fflush (NULL); fprintf (stderr, "%s: %s[%d]: unterminated line\n", execname, filename, linenr); fflush (NULL); exit_status = EXIT_FAILURE; }else { *linepos = '\0'; } linepos = strdup (buffer); assert (linepos != NULL); insert_queue (the_queue, linepos); } } </code></pre> <p>EDIT: Here are the other functions. Sorry, copy and pasted the old source. This is the new one.</p> <pre><code>void insert_queue (queue *this, queue_item_t item) { //Inserts a new item at the end of queue. queue_node *temp = malloc(sizeof (struct queue_node)); temp-&gt;item = item; temp-&gt;link = NULL; if (isempty_queue(this)) this-&gt;front = temp; else this-&gt;rear-&gt;link = temp; this-&gt;rear = temp; } queue_item_t remove_queue (queue *this) { //This removes the first item from queue. assert (! isempty_queue (this)); //Creates temp, and rVal and initializes them. queue_node *temp = this-&gt;front; queue_item_t rVal = temp-&gt;item; //Moves on to the next one. this-&gt;front = this-&gt;front-&gt;link; if (this-&gt;front == NULL) this-&gt;rear = NULL; //Free the unlinked node. free(temp); temp = NULL; return rVal; } </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.
    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