title: VS2019 C6011 Error Dereferencing Null Pointer 'NewNode'
body: <p>Hello? I'm studying Doubly Linked List</p>
  
  <p>Anyway, when I used to put data on node, there were C6011 error
  [:Dereferencing Null Pointer 'NewNode']</p>
  
  <p>So I refered <a href="https://docs.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019" rel="noreferrer">https://docs.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019</a> and</p>
  
  <p>tried to check null value. But It didn't work..</p>
  
  <p>I checked some details on error window like picture I added</p>
  
  <p>They said 'NewNode will be NULL', 'NewNode will be still NULL even if it is dereferenced'</p>
  
  <p>
  <a href="https://i.stack.imgur.com/QFzox.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QFzox.png" alt="enter image description here"></a></p>
  
  <p>Code is like below</p>
  
  <p>1.header file(DoubleLinkedList.h)</p>
  
  <pre><code>#pragma once
  #ifndef DOUBLY_LINKEDLIST_H
  #define DOUBLY_LINKEDLIST_H
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  typedef int ElementType;
  
  typedef struct MyNode
  {
      ElementType Data;
      struct MyNode* Prev;
      struct MyNode* Next;
  }Node;
  
  Node* DLL_CreateNode(ElementType NewData); //1.create node
  void DLL_DestroyNode(Node* Node);//2.destroy node
  void DLL_AppendNode(Node** Head, Node* NewNode);//3.append node
  /*
  4.insert node
  connect
  prev-insert-next  
  */
  void DLL_InsertAfter(Node* Current, Node* NewNode);
  
  
  void DLL_RemoveNode(Node** Head, Node* Remove);//5.remove node
  Node* DLL_GetNodeAt(Node* Head, int location);//6.search node 
  int DLL_GetNodeCount(Node* Head);//7.count the number of nodes
  #endif
  </code></pre>
  
  <p>2.DLL.cpp(which is for header file)</p>
  
  <pre><code>#include "DoubleLinkedList.h"
  
  Node* DLL_CreateNode(ElementType NewData)    
  {
      //create node with memory allocation
      Node* NewNode = (Node*)malloc(sizeof(Node));
  
    //nullptr check for c6011(https://docs.microsoft.com/ko-kr/visualstudio/code-quality/c6011?)
      if (NewNode)
      {
          NewNode-&gt;Data = NewData;
          NewNode-&gt;Prev = NULL;
          NewNode-&gt;Next = NULL;
      }
  
  
      return NewNode;
  }
  
  void DLL_DestroyNode(Node* Node)
  {
      free(Node);
  }
  
  void DLL_AppendNode(Node** Head, Node* NewNode)
  {
      if ((*Head) == NULL)
      {
          *Head = NewNode;
      }
      else
      {
          Node* Tail = (*Head);
  
          while (Tail-&gt;Next != NULL)
          {
              Tail = Tail-&gt;Next;
          } 
          Tail-&gt;Next = NewNode;
          NewNode-&gt;Prev = Tail;
      }
  }
  
  void DLL_InsertAfter(Node* Current, Node* NewNode)
  {
  
      NewNode-&gt;Prev = Current-&gt;Next;
      NewNode-&gt;Next = Current;
  
      if (Current-&gt;Next != NULL)
      {
          Current-&gt;Next-&gt;Prev = NewNode;
          Current-&gt;Next = NewNode;
      }
  }
  
  void DLL_RemoveNode(Node** Head, Node* Remove)
  {
  
      if (*Head == Remove)
      {
  
          *Head = Remove-&gt;Next;
          if (*Head != NULL)
          {
              (*Head)-&gt;Prev = NULL;
          }
          Remove-&gt;Next = NULL;
          Remove-&gt;Prev = NULL;
      }
      else
      {
          Node* Temp = Remove;
  
          if (Remove-&gt;Prev != NULL)
          {
  
              Remove-&gt;Prev-&gt;Next = Temp-&gt;Next;
          }
  
          if (Remove-&gt;Next != NULL)
          {
  
              Remove-&gt;Next-&gt;Prev = Temp-&gt;Prev;
          }
  
          Remove-&gt;Prev = NULL;
          Remove-&gt;Next = NULL;
      }
  }
  
  Node* DLL_GetNodeAt(Node* Head, int location)
  {
      Node* Current = Head;
  
      while (Current != NULL &amp;&amp; (--location) &gt;= 0)
      {
          //--location &gt;=0 is for check index
          Current = Current-&gt;Next;
      }
      return Current;
  }
  
  int DLL_GetNodeCount(Node* Head)
  {
      int count = 0;
  
      Node* Current = Head;
  
      while (Current != NULL)
      {
          Current = Current-&gt;Next;
          count++;
      }
  
      return count;
  }
  </code></pre>
  
  <p>3.Practice file(.c)</p>
  
  <pre><code>#include "DoubleLinkedList.h"
  
  int main()
  {
      int i = 0;
      int Count = 0;
      Node* List = NULL;
      Node* Current = NULL;
      Node* NewNode = NULL;
  
     //create&amp;append nodes
      for (i = 0; i &lt; 5; i++) {
          NewNode = DLL_CreateNode(i);
          DLL_AppendNode(&amp;List, NewNode);
      }
  
  
      Count = DLL_GetNodeCount(List);
      for (i = 0; i &lt; Count; i++) {
          Current = DLL_GetNodeAt(List, i);
          printf("List[%d] : %d\n", i, Current-&gt;Data);
      }
  
  
  
      Current = DLL_GetNodeAt(List, 2);
      NewNode = DLL_CreateNode(3000);
  
      DLL_InsertAfter(Current, NewNode);
  
      Count = DLL_GetNodeCount(List);
      for (i = 0; i &lt; Count; i++) {
          Current = DLL_GetNodeAt(List, i);
          printf("List[%d] : %d\n", i, Current-&gt;Data);
      }
  
      for (i = 0; i &lt; Count; i++) {
          Current = DLL_GetNodeAt(List, 0);
  
          if (Current != NULL) {
              DLL_RemoveNode(&amp;List, Current);
              DLL_DestroyNode(Current);
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Thank you!</p>