内存泄漏/瓦尔格林德问题
Memory Leak/Issues with 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
要完全按照要求回答问题:
手动管理内存分配时,您需要为每个new[]
设置一个delete[]
(同样是 delete
和 new
)。 您分配并记住删除的唯一内存是打乱的单词。 瓦尔格林德抱怨是因为您需要删除:
wordBank
中的所有单词-
wordBank
本身
除非我错过了您的代码中的某些内容,否则程序结束时的这样的东西就足够了:
for (int i = 0; i < numWords; ++i)
{
delete[] wordBank[i];
}
delete[] wordBank;
附言 - 可能值得探索各种编译选项和Valgrind选项,它们可以帮助更准确地查明问题的根源。 如果配置正确,通常可以看到分配泄漏资源的确切行号。
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 赫尔格林德和atomic_flag
- 删除堆后对瓦尔格林德错误感到困惑
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 瓦尔格林德:注意到但未处理的国际奥克特尔;是否有必要处理以及如何找到?
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- 瓦尔格林德错误来自 boost::asio
- 瓦尔格林德可能丢失了,仍然可以到达并被瓦尔格林德检测到并被抑制
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 瓦尔格林德"不支持arch_prctl选项"
- 有没有办法只从瓦尔格林德获得泄漏摘要?
- 瓦尔格林德 + --泄漏检查 -> 不泄漏摘要
- 我找不到瓦尔格林德告诉我的记忆链接
- 直接丢失或间接丢失 - 瓦尔格林德问题
- 内存泄漏/瓦尔格林德问题