C/C++ 中的記憶體洩漏是什麼?


當程式設計師之前分配的一塊記憶體未被程式設計師正確地釋放時,就會發生記憶體洩漏。該記憶體將不再被程式使用。因此,該位置無緣無故被保留。這就是為什麼這被稱為記憶體洩漏。

對於記憶體洩漏,一些記憶體塊可能會浪費。即使系統有足夠的記憶體,這也會降低效能。

示例

void my_func() {
   int *data = new int;
   *data = 50;
}

此處的問題是 *資料指標從未被刪除,因此記憶體被浪費了。

示例

#include <stdio.h>
main(void) {
   auto int my_fun();
   my_fun();
   printf("Main Function\n");
   int my_fun() {
      printf("my_fun function\n");
   }
   printf("Done");
}

輸出

my_fun function
Main Function
Done

更新於: 2019 年 7 月 30 日

3 萬 + 閱讀

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.