(error) Common realloc mistake: ‘p’ nulled but not freed upon failureera エラー:reallocに失敗したが古い領域は解放さ… 続きを読む 【C言語】
reallocのエラー処理でよくあるメモリリーク
カテゴリー: cppcheck
【C言語】
【C言語】
文字列を比較する関数 strcmp()の戻り値は true/falseじゃない
warning: function ‘strcmp’ is compared to a suspicious constant 警告:strcmpの戻り値を0以外と比較するのは疑わしい[misc-… 続きを読む 【C言語】
文字列を比較する関数 strcmp()の戻り値は true/falseじゃない
【C言語】
デッドコード(到達不能コード)
if(x!=0){if(x==0){;}}
(warning): Opposite inner ‘if’ condition leads to a dead code block. 警告:矛盾する逆条件はデッドコードになる[(warning… 続きを読む 【C言語】
デッドコード(到達不能コード)
if(x!=0){if(x==0){;}}
【C言語】
オーバーフローと ラップアラウンドの違い (overflow vs wrap around)
(error): Signed integer overflow for expression ‘y+2‘ 警告:’y+2’の計算で符号付き整数オーバーフロー[(error… 続きを読む 【C言語】
オーバーフローと ラップアラウンドの違い (overflow vs wrap around)
【C言語】
今時(C99仕様)のfor文
変数のスコープを狭くすると
コードを読むのが楽になる
(style):The scope of the variable ‘j’ can be reduced. (スタイル):変数 ‘j’ のスコープを縮小できる。[(styl… 続きを読む 【C言語】
今時(C99仕様)のfor文
変数のスコープを狭くすると
コードを読むのが楽になる
【C言語】
構造体を代入する方法
(メンバを個別代入しなくてOK)
■1.適切な構造体代入の例 同じ種類の構造体は一発で全部代入できます。 ■2.個別代入でメンバーを間違えた例 (warning): Redundant assignment of ‘dst->y’… 続きを読む 【C言語】
構造体を代入する方法
(メンバを個別代入しなくてOK)
【C言語】
64bit gccのintのサイズは
64bit(8byte)じゃない
(style): int result is returned as long value. If the return value is long to avoid loss of information, then … 続きを読む 【C言語】
64bit gccのintのサイズは
64bit(8byte)じゃない
【C言語】
ポインタは負になるか?
(void *)-1の使い方
warning: ordered comparison of pointer with integer zero 警告:整数の0とのポインタの比較[-Wextra] ■1.ポインタは負値にならない 成功時ポインタを返し失… 続きを読む 【C言語】
ポインタは負になるか?
(void *)-1の使い方
【C言語】
memsetのよくある間違い
■1.memsetのよくある間違い ■2.[(warning)memsetZeroBytes] 第3引数のサイズが0 ■3.[(portability)memsetFloat] 第2引数に指定できるのはint型だが実質u… 続きを読む 【C言語】
memsetのよくある間違い
【C言語】
プロトタイプ宣言と関数定義で引数の名前が違う
(style): Function ‘my_memset’ argument 3 names different: declaration ‘element’ defini… 続きを読む 【C言語】
プロトタイプ宣言と関数定義で引数の名前が違う
【C言語】冗長コード
(duplicate,identical,redundant)
(style): Consecutive return, break, continue, goto or throw statements are unnecessary. (スタイル):連続した return, br… 続きを読む 【C言語】冗長コード
(duplicate,identical,redundant)
【C言語】
ifとswitchどっちが良い?
(switch vs else if)
(style): Expression is always false because ‘else if’ condition matches previous condition at line… 続きを読む 【C言語】
ifとswitchどっちが良い?
(switch vs else if)
【C言語】
ビット演算子の不一致 (bitwise,bitmask,bit pattern)
■1.bitwise comparison always evaluates to true ■2.bitwise comparison always evaluates to false ■3.Mismatching … 続きを読む 【C言語】
ビット演算子の不一致 (bitwise,bitmask,bit pattern)
【C言語】
配列の添え字チェックを先に
if(buf[idx] && idx < MAX)
(style) Array index ‘idx’ is used before limits check. 警告:配列添え字を範囲チェック前に使った[(style)arrayIndexThenC… 続きを読む 【C言語】
配列の添え字チェックを先に
if(buf[idx] && idx < MAX)
【C言語】
冗長な条件 if(x > 10 && x > 11)
(Redundant condition)
(style) Redundant condition: If ‘x > 11’, the comparison ‘x > 10’ is always true… 続きを読む 【C言語】
冗長な条件 if(x > 10 && x > 11)
(Redundant condition)
【C言語】ポインタのオーバーフローは未定義動作
(warning) Comparison is wrong. Result of ‘p+1’ can’t be 0 unless there is pointer overflow, … 続きを読む 【C言語】ポインタのオーバーフローは未定義動作
【C言語】
無限ループの書き方
while(1)を避け
for(;;)を使う
■1.無限ループwhile(1)には亜種が多い(非推奨) while(1)から派生した色々な書き方があります。for(;;)の派生は、せいぜいセミコロンの空白くらいです。 ■2.while(1)に文句を言うツール対策(非… 続きを読む 【C言語】
無限ループの書き方
while(1)を避け
for(;;)を使う