用VC寫個小程式作個實驗。 int *a = (int*)malloc(0); if (a) cout else cout 程式執行的結果是, 'not null' 為什麼配置一塊大小為0的記憶體的回傳值不是NULL? 有時候要配置的記憶體大小是計算得到的,這個大小有可能為0,為了和配置失敗回傳的NULL作區分,同時也能保持程式對任何大小記憶體的處理一致,所以就讓0大小的配置也回傳個有效的指標。 不管是不是NULL,free都能接受。 ... 最後,根據 C99標準 : If the size of the space requested is zero, the behavior is implementation-defined : either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. 所以,簡單的講,malloc(0)是沒有定義的行為。對於沒有定義的行為,去討論它是沒意義的...