小さい頃はエラ呼吸

いつのまにやら肺で呼吸をしています。


【cppcheck】error: Deallocating a deallocated pointer: xxx

はじめに

C++の静的解析ツール「cppcheck」でソースコードを静的解析した場合に、以下の警告がでることがあります。

error: Deallocating a deallocated pointer: xxx

cppcheckのバージョン
  • v1.72
サンプルプログラム

以下のソースプログラムを解析にかけると表示されます。

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR *argv[])
{
  char *p = (char *)malloc(100);
  if (p == NULL)
  {
    printf("%s", "メモリアロケート失敗");
    return EXIT_FAILURE;
  }

  free(p);
  free(p); // ->error: Deallocating a deallocated pointer: p

  return EXIT_SUCCESS;
}

上記のように、freeした領域を再度freeしている場合に警告が表示されます。