GOTO語句有著很臭的名聲,我們的老師經常教導我們說,不要輕易使用它。
C++跳轉語句有三個:goto、break和continue。它們只是工具,我覺得問題不能歸咎於工具,問題在於人。
就像指針一樣,goto這個無條件跳轉語句力量還是很強大的,如果濫用,出現問題很難排查。
但有些時候goto確實是不二選擇,例如我遇到的,在函數中有多個出口,而每個出口都遇到釋放資源的時候,與其都把釋放語句不厭其煩的寫一遍,
不如一個goto語句來的干脆利落。
下面的例子取自上一篇Native Wifi API文章,由於我們的程序經常控制的wifi的on和off,必須注意釋放資源。就拿WlanOpenHandle來說,
如果不注意對稱WlanCloseHandler,程序幾次運行後報錯:ERROR_REMOTE_SESSION_LIMIT_EXCEEDED
官網解釋為:Too many handles have been issued by the server.
所以我們會在每個API調用後,確認返回值,如果錯誤,程序將不再繼續向下運行,return之前,我們必須釋放資源。當出口很多時,我們要寫很多同樣的代碼,
很煩躁,難讀,代碼急速膨脹。但使用goto後,問題便輕松了許多,請看簡單例子:
// ManageWirelessNetwork.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include#include #include #include // Need to link with shell32.lib #pragma comment(lib, "shell32.lib") #pragma comment(lib, "wlanapi.lib") int _tmain(int argc, _TCHAR* argv[]) { DWORD dwResult = 0; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; HANDLE hClient = NULL; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult); return false; } dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult); goto RELEASE_RESOURCE; } WLAN_PHY_RADIO_STATE state; state.dwPhyIndex = 0; state.dot11SoftwareRadioState = dot11_radio_state_on;//off here too. PVOID pData = &state; dwResult = WlanSetInterface(hClient,&pIfList->InterfaceInfo[0].InterfaceGuid, wlan_intf_opcode_radio_state,sizeof(WLAN_PHY_RADIO_STATE),pData,NULL); if(dwResult == ERROR_SUCCESS) { wprintf(L"set state success!\n"); } else { wprintf(L"set state failed!err is %d\n",dwResult); } RELEASE_RESOURCE: if(hClient) { WlanCloseHandle(hClient,NULL); hClient = NULL; } if(pIfList) { WlanFreeMemory(pIfList); pIfList = NULL; } if(pIfInfo) { WlanFreeMemory(pIfInfo); pIfInfo = NULL; } return 0; }