2016-03-04 83 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选项,这些选项可以更准确地查明问题的根源。如果你的东西配置正确,通常可以看到分配泄漏资源的确切行号。