内存泄漏/瓦尔格林德问题

Memory Leak/Issues with Valgrind

本文关键字:林德 问题 泄漏 内存      更新时间:2023-10-16

这是我的代码,后跟错误消息,有人可以告诉我出了什么问题吗?

#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

要完全按照要求回答问题:

手动管理内存分配时,您需要为每个new[]设置一个delete[](同样是 deletenew )。 您分配并记住删除的唯一内存是打乱的单词。 瓦尔格林德抱怨是因为您需要删除:

  • wordBank中的所有单词
  • wordBank本身

除非我错过了您的代码中的某些内容,否则程序结束时的这样的东西就足够了:

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

附言 - 可能值得探索各种编译选项和Valgrind选项,它们可以帮助更准确地查明问题的根源。 如果配置正确,通常可以看到分配泄漏资源的确切行号。