2016-03-04 108 views
-1

這裏是我的代碼,然後是錯誤消息,有人可以告訴我什麼是錯的?Valgrind的內存泄漏/問題

#include <bits/stdc++.h> 
using namespace std; 

// wscramble.cpp 
// Word Scramble guessing game 
// Illustrates string library functions, character arrays, 
// arrays of pointers, etc. 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cstring> 
using namespace std; 

// Scramble the letters of this string randomly 
void permute(char items[], int len); 

int main(int argc, char **argv) { 
// check if user has entered enough number of arguments 
if (argc < 2) { 
    cout << "Error: Not enough number of argument entered!" << endl; 
    return 0; 
} 

// create a stream to read file 
ifstream fp(argv[1]); 

// check if file is opened 
if (!fp.is_open()) { 
    cout << "Failed to open file!" << endl; 
    return 0; 
} 

// read number of words to read 
int numWords; 
if (!(fp >> numWords)) { 
    cout << "Error: Failed to read subsequent number of words!" << endl; 
    return 0; 
} 

// allocate memory to array 
char **wordBank; 
wordBank = new char*[numWords]; 

// hold string to read in from file 
char buffer[41]; 

for (int i = 0; i < numWords; ++i) { 
    // read word from the file 
    fp >> buffer; 

    // create new array to hold this 
    char *newWord = new char[strlen(buffer) + 1]; 

    // copy content from buffer to new memory location 
    strcpy(newWord, buffer); 

    // set pointer in wordBank 
    wordBank[i] = newWord; 
} 

// close the file 
fp.close(); 

srand(time(0)); 
char guess[80]; 
bool wordGuessed = false; 
int numTurns = 10; 

// Pick a random word from the wordBank 
int target = rand() % numWords; 
int targetLen = strlen(wordBank[target]); 

// Make a dynamically-allocated copy of the word and scramble it 
char* word = new char[targetLen+1]; 
strcpy(word, wordBank[target]); 
permute(word, targetLen); 

// An individual game continues until a word 
// is guessed correctly or 10 turns have elapsed 
while (!wordGuessed && numTurns > 0) { 
cout << "Scrambled word: " << word << endl; 
cout << "What do you guess the original word is? "; 
cin >> guess; 
wordGuessed = (strcmp(guess, wordBank[target]) == 0); 
numTurns--; 
} 
if (wordGuessed) { 
cout << "You win!" << endl; 
} 
else { 
cout << "Too many turns...You lose!" << endl; 
} 
delete [] word; 
return 0; 
} 
// Scramble the letters. See "Knuth shuffle" on Wikipedia. 
void permute(char items[], int len) { 
for (int i = len-1; i > 0; --i) { 
int r = rand() % i; 
char temp = items[i]; 
items[i] = items[r]; 
items[r] = temp; 
} 
} 

以下是錯誤總結我收到:

==3222== HEAP SUMMARY: 
==3222==  in use at exit: 88 bytes in 7 blocks 
==3222== total heap usage: 10 allocs, 3 frees, 8,854 bytes allocated 
==3222== 
==3222== 88 (48 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 
==3222== at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==3222== by 0x401088: main (in /home/student/cs103/lab-valgrind/scramble) 
==3222== 
==3222== LEAK SUMMARY: 
==3222== definitely lost: 48 bytes in 1 blocks 
==3222== indirectly lost: 40 bytes in 6 blocks 
==3222==  possibly lost: 0 bytes in 0 blocks 
==3222== still reachable: 0 bytes in 0 blocks 
==3222==   suppressed: 0 bytes in 0 blocks 
==3222== 
==3222== For counts of detected and suppressed errors, rerun with: -v 
==3222== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 

我用了命令:$的valgrind --tool = MEMCHECK --leak檢查= YES ./scramble wordbank.txt

+0

請以文本形式發佈錯誤消息,而不是圖像。 –

+1

您在粗略閱讀中發現的唯一分配的資源是文件指針和可憎的是wordBank(這是您無法獲得的資源)。您應該使用'std :: vector wordBank'來重寫它,不需要任何手動資源管理。賠率是問題將會消失。 –

+0

^我該怎麼做? –

回答

1

要準確地回答這個問題的問:

手動管理的內存分配,如你是,你需要有一個delete[]爲每new[](同樣deletenew)。您已經分配並記住要刪除的唯一內存就是亂碼。 Valgrind是抱怨,因爲您需要刪除:

  • 所有wordBank
  • wordBank本身

除非我錯過了你的代碼的東西,這樣的事情在節目結束就足夠了的話:

for (int i = 0; i < numWords; ++i) 
{ 
    delete[] wordBank[i]; 
} 
delete[] wordBank; 

PS - 可能值得探索各種編譯選項和Valgrind選項,這些選項可以更準確地查明問題的根源。如果你的東西配置正確,通常可以看到分配泄漏資源的確切行號。