title: Creating a queue in C and Malloc when adding to the Queue
body: <p>Wanted to check with the community on implementing a queue in C and <code>malloc</code> when adding a new node.  </p>
  
  <p>When I create a <code>node</code>/<code>process</code> in my code, I allocate memory at the point I create the process.  However, when I look at various implementations of a queue, I noticed that when adding to the queue i.e. <code>void push_back(struct*);</code>, I've noticed that some implementations allocate / <code>malloc</code> when adding a new node to the tail of the queue.  If I already allocate the dynamic memory at the point I create the <code>struct</code>, do I need to <code>malloc</code> space when calling the function <code>push_back(struct*)</code>?  My example below does not.  </p>
  
  <pre><code>typedef struct process{
     int pid;
     struct process* next;
  } Process;
  
  typedef struct Queue{
     Process* head;
     Process* tail;
  }Queue;
  
  void push_back(Queue *q, Process *p){
  
      p-&gt;next = NULL;
  
      if( q-&gt;head == NULL &amp;&amp; q-&gt;tail ==NULL){
          q-&gt;head = q-&gt;tail = p;
      }
      else {
          q-&gt;tail-&gt;next = p;
          q-&gt;tail = p;
      }
  }
  </code></pre>