一個關於visual studio的問題?

對之前的問題描述進行了些修改:

代碼如下

void delete_tree(tree *root)
{
if(root==NULL)
return;
delete_tree(root-&>left);
printf("Deleting %d node.
",root-&>data);
free(root);
delete_tree(root-&>right);
}

我知道這種中序遍歷式的delete會有些潛在的問題, 引用stackoverflow 熵 User arduic的一段解釋就是

The code above does not clear the root node after freeing it as such the memory is still all there. As such the C code is able to go to that memory location (which the OS has already received) and modify it. This is EXTREMELY dangerous behaviour because the OS can give this memory to another program and then your program could change causing obviously unexpected behaviour. The simple fix for this would be to remove the right node before freeing the root node.

不過我在這裡想問的是這段代碼在visual studio自帶的編譯器和使用Visual Studio 2017 - Clang with Microsoft CodeGen (v141_clang_c2)都是可以通過編譯的, 但是無法輸出(原因就是已經freel(sth)任何後訪問了sth-&>rchild), 而我用wsl-gcc, MinGW是可以正確輸出所有delete項的, 當然, 到這裡我還是沒什麼疑惑的, 畢竟可能對這種undefined行為可能會有不同的處理方法.

但是我在這兩個網站Visual C++ Compiler Online compile visual studio c++ online都嘗試了在線使用visual c++和clang竟然都是能運行後正確輸出delete順序的的, 所以想在這裡請教下大家why?

補充: 這個問題不是問的delete的思路問題, 我知道應該在最下面free, 然而我想問的是為什麼同樣的編譯器會有不同的表現, 比如上面提到的在線的visual c++/clang能正確輸出所有項, 而我在本地vs中用自帶的編譯器和clang都不能(當然, F5後停在delet_treee處).

這裡的變數是ide對異常的處理嗎?

如圖


明顯free(root)應該放在最下面好嘛,你把root幹掉了,然後去讀root-&>right,是想要怎樣?

不過我沒有看出來這個代碼為什麼不能編譯,你還得貼錯誤信息。


推薦閱讀:

2017年6月,GCC 7.1 對於 C++17 標準的支持情況如何了?
關於VS2015的報錯問題,?
acquire and release semantics in mutex的理解?
貼吧用戶幻之上帝真實的水平怎樣?跟輪子哥比如何?

TAG:C | MicrosoftVisualStudio | GCC | Clang |