title: Different results obtained when using MSVC compiler and GCC compiler.How can I achive the same ouput as gcc in msvc(visual studio)?
body: <p>Trying to run the following shortest path algorithm code. When I tried running the code on visual studio 2019, there was an unexpected output for the same code which I ran on a GCC compiler.  </p>
  
  <p>Is there any way I can get the same output in the MSVC compiler(Visual Studio 2019)?</p>
  
  <p>The code:</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  #define M 10
  #define N 10
  using namespace std;
  
  class Node
  {
  public:
      int x;
      int y;
      int dist;
  
      Node* parent;
  
      Node(int ax, int ay, int adist, Node* aparent)
      {
          x = ax;
          y = ay;
          dist = adist;
          parent = aparent;
      }
  };
  
  void printPath(Node* node)
  {
      if (node == NULL)
      {
          return;
      }
      printPath(node-&gt;parent);
      std::cout &lt;&lt; "{"&lt;&lt; node-&gt;x &lt;&lt; ", " &lt;&lt; node-&gt;y &lt;&lt;"}"&lt;&lt; std::endl;
  }
  
  bool isValid(int** mat, bool** visited, int row, int col)
  {
      return ((row &gt;= 0) &amp;&amp; (row &lt; M) &amp;&amp; (col &gt;= 0) &amp;&amp; (col &lt; N) &amp;&amp;
          (mat[row][col] == 1) &amp;&amp; (!visited[row][col]));
  }
  
  void BFS(int** mat, int i, int j, int x, int y)
  {
  
      int row[4] = { -1, 0, 0, 1 };
      int col[4] = { 0, -1, 1, 0 };
      // construct a matrix to keep track of visited cells
      bool** visited; /*= new bool[M][N]; */
      visited = new bool* [10];
      for (int i = 0; i &lt; 10; i++)
      {
          visited[i] = new bool[10];
          memset(visited[i], false, sizeof(visited[i]));
      }
      // create an empty queue
      std::queue&lt;Node&gt; q;
  
      // mark source cell as visited and enqueue the source node
      visited[i][j] = true;
      q.push(Node(i, j, 0, nullptr));
  
      // stores length of longest path from source to destination
      int min_dist = INT_MAX;
      // run till queue is not empty
      Node* node = NULL;
      while (!q.empty())
      {
          // pop front node from queue and process it
          node = &amp;(q.front());
          q.pop();
          // (i, j) represents current cell and dist stores its
          // minimum distance from the source
          i = node-&gt;x;
          j = node-&gt;y;
          /*cout &lt;&lt; i &lt;&lt; "----- " &lt;&lt; j &lt;&lt; "\n";*/
          int dist = node-&gt;dist;
          if ((i == x) &amp;&amp; (j == y))
          {
              //  cout&lt;&lt;"dasdas";
              min_dist = dist;
              break;
          }
  
          // check for all 4 possible movements from current cell
          // and enqueue each valid movement
          for (int k = 0; k &lt; 4; k++)
          {
              // check if it is possible to go to position
              // (i + row[k], j + col[k]) from current position
              if (isValid(mat, visited, i + row[k], j + col[k]))
              {
  /*                  std::cout  &lt;&lt; ":::::::::::::::::" &lt;&lt; std::endl;*/
                      // mark next cell as visited and enqueue it
                  visited[i + row[k]][j + col[k]] = true;
                  q.push(Node(i + row[k], j + col[k], dist + 1, node));
              }
          }
      }
          //std::cout&lt;&lt;"dasdasdas";
      if (min_dist &lt; INT_MAX)
      {
          std::cout &lt;&lt; "The shortest path from source to destination "
              "has length " &lt;&lt; min_dist&lt;&lt;endl;
                  printPath(node);
  
      }
      else
      {
          std::cout &lt;&lt; "Destination can't be reached from given source";
      }
  }
  
  int main()
  {
      int mat[M][N] = {
          { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
          { 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 },
          { 0, 0, 1, 0, 1, 1, 1, 0, 0, 1 },
          { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
          { 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
          { 1, 0, 1, 1, 1, 0, 0, 1, 1, 0 },
          { 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
          { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
          { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
          { 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 },
      };
  
      int** array2D = 0;
      array2D = new int* [10];
      for (int h = 0; h &lt; 10; h++)
      {
          array2D[h] = new int[10];
      }
  
  
      for (int hs = 0; hs &lt; 10; hs++) {
          for (int ws = 0; ws &lt; 10; ws++) {
              array2D[hs][ws] = mat[hs][ws];
  
          }
  
      }
  
  
      for (int hs = 0; hs &lt; 10; hs++) {
          for (int ws = 0; ws &lt; 10; ws++) {
              std::cout &lt;&lt; array2D[hs][ws] &lt;&lt; " ";
          }
          std::cout &lt;&lt; std::endl;
      }
  
  
      BFS(array2D, 0, 0, 7, 5);
      return 0;
  }
  
  </code></pre>
  
  <p>Expected correct output in <strong>gcc</strong>:</p>
  
  <pre><code>1 1 1 1 1 0 0 1 1 1 
  0 1 1 1 1 1 0 1 0 1 
  0 0 1 0 1 1 1 0 0 1 
  1 0 1 1 1 0 1 1 0 1 
  0 0 0 1 0 0 0 1 0 1 
  1 0 1 1 1 0 0 1 1 0 
  0 0 0 0 1 0 0 1 0 1 
  0 1 1 1 1 1 1 1 0 0 
  1 1 1 1 1 0 0 1 1 1 
  0 0 1 0 0 1 1 0 0 1 
  The shortest path from source to destination has length 12
  {0, 0}
  {0, 1}
  {0, 2}
  {1, 2}
  {2, 2}
  {3, 2}
  {3, 3}
  {4, 3}
  {5, 3}
  {5, 4}
  {6, 4}
  {7, 4}
  {7, 5}
  </code></pre>
  
  <p>MSVC compiler output(incorrect):</p>
  
  <pre><code>1 1 1 1 1 0 0 1 1 1
  0 1 1 1 1 1 0 1 0 1
  0 0 1 0 1 1 1 0 0 1
  1 0 1 1 1 0 1 1 0 1
  0 0 0 1 0 0 0 1 0 1
  1 0 1 1 1 0 0 1 1 0
  0 0 0 0 1 0 0 1 0 1
  0 1 1 1 1 1 1 1 0 0
  1 1 1 1 1 0 0 1 1 1
  0 0 1 0 0 1 1 0 0 1
  Destination can't be reached from given source
  </code></pre>