2017-12-05 18 views
-2

私はCプログラムを書いて、挿入ソートとカウントソートの実行時間を比較しましたが、このプログラムは初期化されていないメモリから読み込むため、valgrindテストに合格できません。valgrindのテストによるメモリ管理の障害?

void count_sort_write_output_array(int output_array[], int len, int count_array[], int* befehle) { 
    int k=0; 
    //use count array to output result 
    for (int j=0;j<=MAX_VALUE;j++) { 
     //add befehle 
     //(j++) 
     (*befehle)++; 
     for (int i=0; i<count_array[j]; i++) { 
      output_array[k] = j; 
      k++; 
      //add befehle 
      //(i++, output_array[k] = j, k++) 
      (*befehle)+=3; 
     } 
    } 
} 

void count_sort(int array[], int len, int* befehle) { 
    int* count_array = malloc(sizeof(int) * MAX_VALUE); 

    //fill count_array with 0 
    for(int i=0;i<MAX_VALUE;i++){ 
     count_array[i] = 0; 
    } 

    count_sort_calculate_counts(array, len, count_array, befehle); 

    //use output array to save result 
    count_sort_write_output_array(array, len, count_array, befehle); 

    free(count_array); 

} 

、ここではvalgrindの結果である:

==6694== Memcheck, a memory error detector 
==6694== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==6694== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info 
==6694== Command: ./aaaad 
==6694== 
==6694== Invalid read of size 4 
==6694== at 0x4009ED: count_sort_write_output_array (in /afs/tu-berlin.de/home/r/ryoine/irb-ubuntu/introprog-wise1718/Tutorien/t24/Studierende/ryoine/Abgaben/Blatt03/aaaad) 
==6694== by 0x400A8D: count_sort (in /afs/tu-berlin.de/home/r/ryoine/irb-ubuntu/introprog-wise1718/Tutorien/t24/Studierende/ryoine/Abgaben/Blatt03/aaaad) 
==6694== by 0x400FD5: main (in /afs/tu-berlin.de/home/r/ryoine/irb-ubuntu/introprog-wise1718/Tutorien/t24/Studierende/ryoine/Abgaben/Blatt03/aaaad) 
==6694== Address 0x6916d40 is 0 bytes after a block of size 20,000,000 alloc'd 
==6694== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6694== by 0x400A2A: count_sort (in /afs/tu-berlin.de/home/r/ryoine/irb-ubuntu/introprog-wise1718/Tutorien/t24/Studierende/ryoine/Abgaben/Blatt03/aaaad) 
==6694== by 0x400FD5: main (in /afs/tu-berlin.de/home/r/ryoine/irb-ubuntu/introprog-wise1718/Tutorien/t24/Studierende/ryoine/Abgaben/Blatt03/aaaad) 
==6694== 
Parameter MAX_VALUE hat den Wert 5000000 
           Countsort        Insertionsort 
     n     Befehle   Laufzeit    Befehle   Laufzeit 
    10000     5050001   425.5080    50215642  1929.5740  
    20000     5100001   423.1040    200387176  7655.5280  
    30000     5150001   427.7080    453403054  16763.7200  
    40000     5200001   363.3830    797737482  28034.7020  
    50000     5250001   392.9350    1245274822  44438.0240  
==6694== 
==6694== HEAP SUMMARY: 
==6694==  in use at exit: 0 bytes in 0 blocks 
==6694== total heap usage: 26 allocs, 26 frees, 101,224,264 bytes allocated 
==6694== 
==6694== All heap blocks were freed -- no leaks are possible 
==6694== 
==6694== For counts of detected and suppressed errors, rerun with: -v 
==6694== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0) 
+3

明らかにあなたが投稿した部分が問題と一部ではありません。 Valgrindからラインリファレンスを取得する必要があります。これは非常に便利です。 – unwind

+1

valgrindの出力は、問題のメモリアクセスが 'count_sort_write_output_array'の範囲内にあることを示しています。なぜそれが問題にならないのですか? – grek40

+1

この 'count_sort_write_output_array'メソッドを投稿する – coderredoc

答えて

2

ここにあなたの問題があります:

ここ はおそらく偽である、と私はこの問題を持っている理由を知りたい可能性が私のコードであり、
for (int j=0;j<=MAX_VALUE;j++) { 

MAX_VALUEintの配列のスペースを割り当てます。したがって、この配列のインデックスの範囲は0からMAX_VALUE - 1までです。しかし、あなたのループはjの範囲をMAX_VALUEにすることができます。その場合、count_array[j]は配列の最後から1つの要素を読み込んでいます。

にあなたのループ条件を修正MAX_VALUEが含まれていない:

for (int j=0;j<MAX_VALUE;j++) {