内存泄漏/ Valgrind的问题

时间:2016-03-04 19:21:42

标签: c++ 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-check = yes ./scramble wordbank.txt

1 个答案:

答案 0 :(得分:1)

完全按照要求回答问题:

当您手动管理内存分配时,您需要为delete[] new[] deletenew提供wordBank。您已分配并记住要删除的唯一内存是加扰字。 Valgrind抱怨是因为你需要删除:

  • wordBank
  • 中的所有字词
  • for (int i = 0; i < numWords; ++i) { delete[] wordBank[i]; } delete[] wordBank; 本身

除非我在代码中遗漏了某些内容,否则程序末尾的内容应该足够了:

    var titleMovieTmpl = _.template('
        <h4 style="display: inline-block;">
            <%= item.title %> (<%= item.year %>)
        </h4>');

P.S。 - 可能值得探索各种编译选项和Valgrind选项,以帮助更准确地找出问题的根源。如果你正确配置了东西,通常可以看到分配泄漏资源的确切行号。