Oracle® Developer Studio 12.5: コードアナライザユーザーズガイド

印刷ビューの終了

更新: 2016 年 6 月
 
 

動的メモリーアクセスの警告

動的メモリーアクセス検査では、次の種類の警告が検出されます。

  • AZS: 0 サイズの割り当て (allocating zero size)

  • メモリーリーク

  • SMR: 投機的な非初期化メモリーからの読み取り (speculative uninitialized memory read)

このセクションでは、警告の考えられる原因と、警告が発生する可能性があるコードの例を説明します。

サイズ 0 の割り当て (AZS)

例:

    #include <stdlib>
    int main()
    {
      int *p = malloc(); // Allocating zero size memory block
    }

メモリーリーク (MLK)

考えられる原因: メモリーが割り当てられるが、関数の終了またはエスケープの前に解放されていません。

例:

    int foo()
    {
     int *p = (int*) malloc(sizeof(int));
     if (x) {
      p = (int *) malloc(5*sizeof(int));  // will cause a leak of the 1st malloc
     }  
    }                                     // The 2nd malloc leaked here

投機的なメモリーの読み取り (SMR)

例:

    int i;
    if (foo(&i) != 0)  /* foo returns nonzero if it has initialized i */
    printf("5d\n", i);

コンパイラは、前述のソースに対して次の同等のコードを生成する可能性があります。

    int i;
    int t1, t2'
    t1 = foo(&i);
    t2 = i; /* value in i is loaded. So even if t1 is 0, we have uninitialized read due to speculative load */
    if (t1 != 0) 
    printf("%d\n", t2);