メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MISRA C:2012 Rule 21.3

The memory allocation and deallocation functions of <stdlib.h> shall not be used

説明

ルール定義

The memory allocation and deallocation functions of <stdlib.h> shall not be used 1 .

根拠

メモリ割り当ておよび割り当て解除のルーチンを使用すると、未定義の動作が発生する可能性があります。次に例を示します。

  • 動的に割り当てられなかったメモリを解放する。

  • 解放されたメモリの場所を指すポインターを使用する。

Polyspace 実装

チェッカーは、関数 callocmallocreallocaligned_alloc、および free の使用にフラグを設定します。

マクロにこれらの動的ヒープ メモリ割り当て関数と同じ名前を定義して、コード内のマクロを展開する場合、ルールに違反します。ルール 21.2 に違反していないことを前提としています。

レポート内の追加のメッセージ

  • The macro <name> shall not be used.

  • Identifier XX should not be used.

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

#include <stdlib.h>

static int foo(void);

typedef struct struct_1 {
    int a;
    char c;
} S_1;

static int foo(void) {

    S_1 * ad_1;
    int  * ad_2;
    int  * ad_3;

    ad_1 = (S_1*)calloc(100U, sizeof(S_1));        /* Non-compliant */
    ad_2 = malloc(100U * sizeof(int));             /* Non-compliant */
    ad_3 = realloc(ad_3, 60U * sizeof(long));      /* Non-compliant */

    free(ad_1);                                    /* Non-compliant */
    free(ad_2);                                    /* Non-compliant */
    free(ad_3);                                    /* Non-compliant */

    return 1;
}

この例では、関数 malloccallocrealloc および free が使用される場合、ルールに違反します。

チェック情報

グループ: 標準ライブラリ
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

R2014b で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.