2017-02-05 21 views
-7
私は、アプリケーションコードに delete[]の文から、ヒープバッファの終了後にメモリに

ヒープの破損は、通常のブロック(#191)の後に検出された

を書いたことを

CRTが検出されたレポートを取得するのはなぜ

以下?

は、ここでは、コードです:

#include <stdlib.h> 

int *dynamic(int n) { // making mem allocation; 
    int *mass = new int[n]; 
    return mass; 

} 

int *dynamic_create(int *mass, int n) { // writing in massive random numbers 
    int *mass_aux = mass; 
    for (int *i = mass; i < mass + n; i++) 
     *i = rand()%10; 
    return mass_aux; 
} 


void dynamic_add_last(int *mass, int n, int number) { // adding one element 
    for (int *i = mass; i < mass + n + 1; i++) 
     if (i - mass == n) *i = number; 
} 

int *dynamic_add(int *mass, int n, int number, int pos) 
{ 
    int *mass_aux = new int [n]; 
    int *mass_plus = mass; 
    for (int *i = mass_aux; i < mass_aux + n + 1; i++) { 
     if (i - mass_aux == pos) { 
      *i = number; 
     } else { 
      *i = *mass_plus; 
      mass_plus++; 
     } 
    } 
    delete [] mass; // <-- PROBLEM REPORTED HERE 
    return mass_aux; 
} 

#include <iostream> 

int main() { 
    using std::cin; 
    using std::cout; 
    using std::endl; 

    srand(time(0)); 
    int n; 
    cout << "Massive size: "; cin >> n; 
    int *mass = dynamic(n); 
    int *mass2 = dynamic_create(mass, n); 
    for (int *i = mass2; i < mass2 + n; i++) 
     cout << (i - mass2) << " - " << *i << endl; 
    int s; 
    cout << "Number what you want to add: "; cin >> s; 
    dynamic_add_last(mass, n, s); 
    n++; 
    for (int *i = mass2; i < mass2 + n; i++) 
     cout << (i - mass2) << " - " << *i << endl; 
    int pos; 
    cout << "Position: "; cin >> pos; 
    cout << "Number what you want to add: "; cin >> s; 
    int *mass3 = dynamic_add(mass, n, s, pos); 
    n++; 
    for (int *i = mass3; i < mass3 + n; i++) 
     cout << (i - mass3) << " - " << *i << endl; 

} 
+0

あなた '削除[] mass'は、サンプルコードで示されていない割り当てのためである - 破損がそもそも行われたところ、我々が知ることができません(このコードではない)。 – Niall

+0

std :: vectorを使用した場合、無効なインデックスにアクセスするときに、デバッガから通知が返されます。 –

+1

答えて

0

実際にコンパイルすることができ、コードを持っているあなたの質問を編集したので、私はValgrindのからいくつかのレポートを取得します。最初のものを撮影:他の報告されたエラーのために同様に

for (int *i = mass; i < mass + n + 1; i++) 

==6801== Invalid write of size 4 
==6801== at 0x108D49: dynamic_add_last(int*, int, int) (15111518.cpp:19) 
==6801== by 0x108F47: main (15111518.cpp:57) 
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4) 
==6801== by 0x108E6D: main (15111518.cpp:51) 

かなり明確に、あなたはn整数、今それらのn+1にアクセスしようとしているが割り当てられています。あなたの環境はおそらく、配列が削除されたときにチェックされるカナリアを提供します。そのため、あなたのレポートはその行を参照しています。境界線を越えて書いているところを見るには、コードの残りの部分を調べる必要があります。参考のために、ここで私が入力2 4 3 3となったValgrindのエラーです:

==6801== Memcheck, a memory error detector 
==6801== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==6801== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info 
==6801== Command: ./15111518 
==6801== 
Massive size: 0 - 9 
1 - 2 
Number what you want to add: ==6801== Invalid write of size 4 
==6801== at 0x108D49: dynamic_add_last(int*, int, int) (15111518.cpp:19) 
==6801== by 0x108F47: main (15111518.cpp:57) 
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4) 
==6801== by 0x108E6D: main (15111518.cpp:51) 
==6801== 
0 - 9 
1 - 2 
==6801== Invalid read of size 4 
==6801== at 0x108F77: main (15111518.cpp:60) 
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4) 
==6801== by 0x108E6D: main (15111518.cpp:51) 
==6801== 
2 - 4 
Position: Number what you want to add: ==6801== Invalid read of size 4 
==6801== at 0x108DF1: dynamic_add(int*, int, int, int) (15111518.cpp:31) 
==6801== by 0x10903E: main (15111518.cpp:64) 
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4) 
==6801== by 0x108E6D: main (15111518.cpp:51) 
==6801== 
==6801== Invalid write of size 4 
==6801== at 0x108DE9: dynamic_add(int*, int, int, int) (15111518.cpp:28) 
==6801== by 0x10903E: main (15111518.cpp:64) 
==6801== Address 0x5a8515c is 0 bytes after a block of size 12 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108D89: dynamic_add(int*, int, int, int) (15111518.cpp:24) 
==6801== by 0x10903E: main (15111518.cpp:64) 
==6801== 
0 - 9 
1 - 2 
2 - 4 
==6801== Invalid read of size 4 
==6801== at 0x109072: main (15111518.cpp:67) 
==6801== Address 0x5a8515c is 0 bytes after a block of size 12 alloc'd 
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==6801== by 0x108D89: dynamic_add(int*, int, int, int) (15111518.cpp:24) 
==6801== by 0x10903E: main (15111518.cpp:64) 
==6801== 
3 - 3 
==6801== 
==6801== HEAP SUMMARY: 
==6801==  in use at exit: 12 bytes in 1 blocks 
==6801== total heap usage: 5 allocs, 4 frees, 77,844 bytes allocated 
==6801== 
==6801== LEAK SUMMARY: 
==6801== definitely lost: 12 bytes in 1 blocks 
==6801== indirectly lost: 0 bytes in 0 blocks 
==6801==  possibly lost: 0 bytes in 0 blocks 
==6801== still reachable: 0 bytes in 0 blocks 
==6801==   suppressed: 0 bytes in 0 blocks 
==6801== Rerun with --leak-check=full to see details of leaked memory 
==6801== 
==6801== For counts of detected and suppressed errors, rerun with: -v 
==6801== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0) 
関連する問題