2014-02-28 80 views
0

我在使用的valgrind對此我無法調試得到以下錯誤時,遇到了int變量的指針無效讀取尺寸誤差。使用的valgrind

Memcheck, a memory error detector 
==9215== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. 
==9215== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info 
==9215== Command: ./memleak 1 
==9215== 
==9215== Invalid read of size 4 
==9215== at 0x400CEC: test1(int*, int) (memleak.cpp:11) 
==9215== by 0x400F0F: main (memleak.cpp:55) 
==9215== Address 0x5a1a05c is 0 bytes after a block of size 28 alloc'd 
==9215== at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==9215== by 0x400EAD: main (memleak.cpp:51) 
==9215== 
Test 1 sum is 475 
==9215== 
==9215== HEAP SUMMARY: 
==9215==  in use at exit: 0 bytes in 0 blocks 
==9215== total heap usage: 1 allocs, 1 frees, 28 bytes allocated 
==9215== 
==9215== All heap blocks were freed -- no leaks are possible 
==9215== 
==9215== For counts of detected and suppressed errors, rerun with: -v 
==9215== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2) 

我的代碼是:

#include <iostream> 
#include <cstdlib> 
#include <cstring> 
using namespace std; 

// sum an array of numbers 
int test1(int *data, int len) 
{ 
    int sum = 0; 
    for(int i=0; i <= len; i++){ 
    sum += data[i]; 
    } 
    return sum; 
} 

// Allocate a random number array 
char* test2_init() 
{ 
    char buf[80]; 
    cout << "Enter a word: "; 
    cin >> buf; 
    char* mydat = new char[strlen(buf)+1]; 
strcpy(mydat, buf); 
return mydat; 
} 
char* test2_reverse(char* word) 
{ 
    int len = strlen(word); 
    char* otherword = new char[len+1]; 

    for(int i=0; (unsigned)i < strlen(word); i++){ 
    otherword[i] = word[len-i-1]; 
    } 
    otherword[len+1] = '\0'; 
    delete [] word; 
    return otherword; 
} 

int main(int argc, char* argv[]) 
{ 
    if(argc < 2){ 
    cerr << "Please enter the test number you want to run [1-2]" << endl; 
    return 1; 
    } 
    const int len = 7; 
    int test = atoi(argv[1]); 

    if(test == 1){ 
    // Test should sum up the array values and print it 
    int *data = 0; 
    data=new int[len]; 
    for(int i=0; i < len; i++){ 
     data[i] = rand()%100; 
    } 
    int sum = test1(data, len); 
    delete [] data; 
    cout << "Test 1 sum is " << sum << endl; 
    } 

    else if(test == 2){ 
    // Test should create a random string & then reverse a copy 
    char* myword = test2_init(); 
    cout << "myword is " << myword << endl; 

    char* otherword = test2_reverse(myword);  
    cout << "otherword is " << otherword << endl; 

    delete [] myword; 
    delete [] otherword; 
    } 
    else { 
    cout << "Unknown test number" << endl; 
    } 

    return 0; 
} 

摸不清尺寸爲4個錯誤的無效讀的原因。 我知道我應該使用矢量,但我想爲這個項目使用新的。

+1

除了答案你有喲你的問題,我認爲你有一個雙刪除myword,如果測試== 2. –

+0

是的,甚至沒有看到。反轉字符串的函數不應該釋放其輸入。如果我通過了'char uhoh [] =「不要刪除我」? –

回答

2
for(int i=0; i <= len; i++) 
       ^

訪問所有指標達到和包括lenlen本身已經超過了數組的末尾,或者正如Valgrind所說的那樣,「一個數據塊之後有0個字節」。測試應該是規範i < len

+0

感謝您對這樣一個微小錯誤的迅速回復。 –

1
for(int i=0; i <= len; i++){ 
    sum += data[i]; 
} 

應該

for(int i=0; i < len; i++){ 
    sum += data[i]; 
} 

您正在閱讀一個過去的數組的末尾。指數0...len是有效的,data[len]不是。