<目次>
(1) ValgrindのPossibly Lostの意味や実際のサンプルをご紹介
(1-1) Valgrindの「Possibly Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
(1-3) 例1:Valgrindのメモリリーク確認結果
(1) ValgrindのPossibly Lostの意味や実際のサンプルをご紹介
本記事ではValgrindにおけるメモリリークのカテゴリの1つである「Possibly Lost」についてご紹介します。
(1-1) Valgrindの「Possibly Lost」はどんな状況?
(1-2) 例1:サンプルプログラム
#include <stdlib.h>
int *x1;
int main(void)
{
//# int型の配列(4要素)を動的に確保
int *i = new int[4];
//# 配列の各要素に値を格納
i[0] = 10;
i[1] = 11;
i[2] = 12;
i[3] = 13;
//# グローバル変数のポインタ「*x1」のアドレスに、iの2要素先
//# つまり配列の3要素目のアドレスを代入する
x1 = i+2;
return 0;
}

(1-3) 例1:Valgrindのメモリリーク確認結果
==2374== HEAP SUMMARY: ==2374== in use at exit: 16 bytes in 1 blocks ==2374== total heap usage: 1 allocs, 0 frees, 16 bytes allocated ==2374== ==2374== 16 bytes in 1 blocks are possibly lost in loss record 1 of 1 ==2374== at 0x4C2C866: operator new[](unsigned long) (vg_replace_malloc.c:579) ==2374== by 0x40059E: main (memorytest_possibly.cpp:7) ==2374== ==2374== LEAK SUMMARY: ==2374== definitely lost: 0 bytes in 0 blocks ==2374== indirectly lost: 0 bytes in 0 blocks ==2374== possibly lost: 16 bytes in 1 blocks ==2374== still reachable: 0 bytes in 0 blocks ==2374== suppressed: 0 bytes in 0 blocks ==2374== ==2374== For lists of detected and suppressed errors, rerun with: -s ==2374== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

valgrind --tool=memcheck --leak-check=yes --show-possibly-lost=no ../チェック対象プログラム