はじめに
C++の静的解析ツール「cppcheck」でソースコードを静的解析した場合に、以下の警告がでることがあります。
error: Memory is allocated but not initialized: xxx
cppcheckのバージョン
- v1.66
サンプルプログラム
以下のソースプログラムを解析にかけると表示されます。
#include "stdafx.h" #include <windows.h> #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> int _tmain(int argc, _TCHAR *argv[]) { char* hoge; hoge = (char*)malloc(10); // -> error: Memory is allocated but not initialized: hoge //memset(hoge, 0, 10); // -> OK char szBuff[256] = { 0 }; printf("%s\n", hoge); _CrtDumpMemoryLeaks(); return 0; }
上記のように、malloc関数を呼び出した際、アロケートした領域を初期化していない場合に警告が出力されます。
関連記事
- 【cppcheck】(warning) %d in format string (no. 1) requires 'int' but the argument type is 'ULONG {aka unsigned long}'.
- 【VC++】OCIを使ってOracle DBに接続するコード書いたよ。
- 【cppcheck】(warning) sprintf format string requires 1 parameter but 2 are given.
- 【cppcheck】(style) Clarify calculation precedence for '+' and '?'.
- 【cppcheck】error: Undefined behavior: Variable 'xxx' is used as parameter and destination in s[n]printf().