title: G++ and CL(VS2017) both compiled successfully but the cl-compiled executable run abnormally
body: <p>I write a LinkedList class and a test function to test my class. However, I met some problems while compiling and running my program. There are three files: <code>test.cpp</code> <code>LinkedList.cpp</code> and <code>LinkedList.h</code>.  </p>
  
  <p>test.cpp:</p>
  
  <pre><code>#include "LinkedList.cpp"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  // Loading Array into Linked List for testing
  template &lt;class T, size_t n&gt;
  void fillArrayIn(LinkedList&lt;T&gt; &amp;list, T (&amp;arr)[n])
  {
       unsigned int length = sizeof(arr) / sizeof(*arr);
       for (unsigned int i = 0; i &lt; length; i++)
       {
            list.append(arr[i]);
       }
  }
  
  int main()
  {
  
       // Integer LinkedList
       cout &lt;&lt; "=====================================" &lt;&lt; endl;
       cout &lt;&lt; "Test for Integer LinkedList" &lt;&lt; endl
            &lt;&lt; endl;
       LinkedList&lt;int&gt; intList;
  
       cout &lt;&lt; "Is List Empty? " &lt;&lt; (intList.isEmpty() ? "Yes" : "No") &lt;&lt; endl
            &lt;&lt; endl; // Test isEmpty()
  
       int intArray[] = {1, 2, 5, 6, 9, 0, 1};
       fillArrayIn(intList, intArray); // Test appending
       cout &lt;&lt; "Array Loaded" &lt;&lt; endl;
       cout &lt;&lt; "Is List Empty? " &lt;&lt; (intList.isEmpty() ? "Yes" : "No") &lt;&lt; endl;
       intList.printAll();
  
       intList.insert(*(new int(100)), 3); // Test insertion
       cout &lt;&lt; endl
            &lt;&lt; "Insert 100 to position 3" &lt;&lt; endl;
       intList.printAll();
       cout &lt;&lt; "Insert integer outside the List: "
            &lt;&lt; (intList.insert(*(new int(100)), 100) ? "Success" : "Fail") &lt;&lt; endl
            &lt;&lt; endl; // Test illegal insertion
  
       cout &lt;&lt; "Find the position of integre 9: " &lt;&lt; intList.positionOf(9) &lt;&lt; endl; // Test get the position of element in List
       cout &lt;&lt; "Find the position of not existed element: " &lt;&lt; intList.positionOf(20) &lt;&lt; endl
            &lt;&lt; endl; // Test get the position of not existed element
  
       cout &lt;&lt; "The element at position 0 is " &lt;&lt; *(intList.get(0)) &lt;&lt; endl; // Test get element
       cout &lt;&lt; "The element at position 7 is " &lt;&lt; *(intList.get(7)) &lt;&lt; endl;
       cout &lt;&lt; "The element at position 8 is " &lt;&lt; intList.get(8) &lt;&lt; endl
            &lt;&lt; endl;
  
       cout &lt;&lt; "Deleting 100 from list: "
            &lt;&lt; (intList.remove(*(new int(100))) ? "Sucess" : "Fail") &lt;&lt; endl;
       intList.printAll();
       cout &lt;&lt; "Deleting not existed element from list: "
            &lt;&lt; (intList.remove(*(new int(20))) ? "Sucess" : "Fail") &lt;&lt; endl;
       intList.printAll();
  }
  </code></pre>
  
  <p>LinkedList.cpp:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "LinkedList.h"
  using namespace std;
  
  template &lt;class T&gt;
  LinkedList&lt;T&gt;::LinkedList()
  {
      head = new Node(NULL);
  }
  
  template &lt;class T&gt;
  void LinkedList&lt;T&gt;::append(T &amp;_data)
  {
      Node *current = head;
      while (current-&gt;next)
      {
          current = current-&gt;next;
      }
      current-&gt;next = new Node(&amp;_data);
      // cout &lt;&lt; _data &lt;&lt;" has been appended to the list."&lt;&lt;endl;
  }
  
  template &lt;class T&gt;
  bool LinkedList&lt;T&gt;::insert(T &amp;_data, const int position)
  {
      Node *current = head;
      Node *previous = head;
      for (int i = position + 1; i &gt; 0; i--)
      {
          if (!current-&gt;next)
              return false;
          previous = current;
          current = current-&gt;next;
      }
      previous-&gt;next = new Node(&amp;_data);
      previous-&gt;next-&gt;next = current;
      return true;
  }
  
  template &lt;class T&gt;
  T *LinkedList&lt;T&gt;::get(int position)
  {
      Node *current = head;
      for (int i = position + 1; i &gt; 0; i--)
      {
          if (!current-&gt;next)
              return NULL;
          current = current-&gt;next;
      }
      return current-&gt;data;
  }
  
  template &lt;class T&gt;
  bool LinkedList&lt;T&gt;::isEmpty()
  {
      if (head-&gt;next)
          return false;
      return true;
  }
  
  template &lt;class T&gt;
  bool LinkedList&lt;T&gt;::remove(const T &amp;_data)
  {
      Node *current = head;
      Node *previous = current;
      while (current-&gt;next)
      {
          previous = current;
          current = current-&gt;next;
          if (*(current-&gt;data) == _data)
          {
              previous-&gt;next = current-&gt;next;
              return true;
          }
      }
      return false;
  }
  
  template &lt;class T&gt;
  int LinkedList&lt;T&gt;::positionOf(const T &amp;_data)
  {
      Node *current = head;
      unsigned int position = -1;
      while (current-&gt;next)
      {
          current = current-&gt;next;
          position++;
          if (*(current-&gt;data) == _data)
              return position;
      }
      return -1;
  }
  
  template &lt;class T&gt;
  void LinkedList&lt;T&gt;::printAll()
  {
      Node *current = head;
      while (current-&gt;next)
      {
          current = current-&gt;next;
          cout &lt;&lt; *(current-&gt;data) &lt;&lt; "  ";
      }
      cout &lt;&lt; endl;
  }
  
  template &lt;class T&gt;
  class LinkedList&lt;T&gt;::Node
  {
    public:
      Node(T *_data) : data(_data) {}
      T *data;
      Node *next;
  };
  </code></pre>
  
  <p>LinkedList.h:</p>
  
  <pre><code>#ifndef LINKEDLIST_H
  #define LINKEDLIST_H
  
  template &lt;class T&gt;
  class LinkedList
  {
    public:
      LinkedList();
      void append(T &amp;);
      bool insert(T &amp;, const int);
      T *get(int);
      bool isEmpty();
      bool remove(const T &amp;);
      int positionOf(const T &amp;);
      void printAll();
  
    private:
      class Node;
      Node *head;
  };
  
  #endif
  </code></pre>
  
  <p>When I compile my program with <code>g++</code>, it compiled successfully and give out the expected result:</p>
  
  <pre><code>=====================================
  Test for Integer LinkedList
  
  Is List Empty? Yes
  
  Array Loaded
  Is List Empty? No
  1  2  5  6  9  0  1
  
  Insert 100 to position 3
  1  2  5  100  6  9  0  1
  Insert integer outside the List: Fail
  
  Find the position of integre 9: 5
  Find the position of not existed element: -1
  
  The element at position 0 is 1
  The element at position 7 is 1
  The element at position 8 is 0
  
  Deleting 100 from list: Sucess
  1  2  5  6  9  0  1
  Deleting not existed element from list: Fail
  1  2  5  6  9  0  1
  </code></pre>
  
  <p>When I try to compile my program with <code>cl</code> (VS2017), the compilation is also successful. However, when I run the program, it will be stuck at a certain point. Running result:</p>
  
  <pre><code>=====================================
  Test for Integer LinkedList
  
  Is List Empty? No
  </code></pre>
  
  <p>Later, I try to create a project in Visual Studio. Surprisingly, this time, it runs without any problem.
  How can I make it correct to use <code>cl</code> to compile? Thank you if you could give me some hints! I am a new learner in C++.</p>
  
  <p>B.T.W. The object file and executable generated by <code>cl</code> are more than 200KB, which is much more than the ones <code>g++</code> generated which are around 14KB.</p>