title: Segmentation fault in linked list implementation in C++
body: <p>I am writing a function in C++ to add a "data" of type 'int' to the end of a linked list.</p>
  
  <pre><code>void insert_back()
  {
   int no;
   node *temp;
   cout&lt;&lt;"\nEnter the number"&lt;&lt;"\n";
   cin&gt;&gt;no;
   temp = head;
   if(temp != NULL)
   {
           while(temp != NULL)
                      temp = temp-&gt;next;
   }
  
   temp-&gt;next = (node*)malloc(sizeof(node));
   temp = temp-&gt;next;
   temp-&gt;data = no;
   temp-&gt;next = NULL;
  </code></pre>
  
  <p>}</p>
  
  <p>However, at the line, temp->next = (node*)malloc(sizeof(node)), I get an access violation error(segmentation fault). I do not find anything fundamentally wrong. Can you please enlighten me on the issue?</p>