2017-02-05 178 views
-7

爲什麼我得到的報告堆損壞檢測

CRT檢測到的應用程序從delete[]語句代碼寫的內存堆緩衝區結束後

下面?

下面的代碼:

#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

i

回答

0

已經編輯你的問題有,實際上可以被編譯的代碼,我從Valgrind的幾份報告。以第一種:

==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

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

類似的其他報告的錯誤。您的環境可能提供了一個在數組被刪除時進行檢查的金絲雀,這就是您的報告引用該行的原因。您需要仔細查看代碼的其餘部分,以查看您寫入的範圍。作爲參考,這裏的Valgrind的錯誤,我輸入了2 4 3 3

==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)