Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a queue in C
    text
    copied!<p>I am trying to implement a custom queue for Gstreamer buffers. The problem is that when I try to dequeue, it seems that I am loosing the head of the queue. Whenever I try to dequeue twice, I get a segmentation fault. I have also noticed that head is always equal than head->next. Now I'm not sure if there's something wrong with enqueue or dequeue. Please help me out. Thank you.</p> <pre><code>typedef struct _GstBUFFERQUEUE GstBufferQueue; struct _GstBUFFERQUEUE { GstBuffer *buf; guint buf_size; struct _GstBUFFERQUEUE *next; }; void enqueue_gstbuffer(GstBufferQueue **head, GstBufferQueue **tail, guint *queue_size, GstBuffer *buf) { if (*queue_size == 0) { *head = malloc(sizeof(GstBufferQueue)); (*head)-&gt;buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE(buf)); (*head)-&gt;buf = gst_buffer_copy(buf); *tail = *head; } else { if ((*tail)-&gt;next = malloc(sizeof(GstBufferQueue))) { (*tail)-&gt;next-&gt;buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE(buf)); (*tail)-&gt;next-&gt;buf = gst_buffer_copy(buf); (*tail) = (*tail)-&gt;next; } else { GST_WARNING("Error allocating memory for new buffer in queue"); } } (*tail)-&gt;next = NULL; (*queue_size)++; } void dequeue_gstbuffer(GstBufferQueue **head, GstBufferQueue **tail, guint *queue_size, GstBuffer **buf) { GstBufferQueue **tmpPtr = head; GstBufferQueue **nextPtr; *nextPtr = (*head)-&gt;next; *buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE((*tmpPtr)-&gt;buf)); *buf = gst_buffer_copy((*tmpPtr)-&gt;buf); gst_buffer_unref((*tmpPtr)-&gt;buf); free((*tmpPtr)); *head = *nextPtr; if ((*head) == NULL) (*tail) = NULL; (*queue_size)--; } </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