[英]Am i deleting incorrectly?
因此,在课堂上,我们必须创建一个从文件中获取单词,忽略标点符号并打印出单词的代码。 不仅如此,它还必须忽略同一单词的副本,但要跟踪哪些单词重复并将其计数添加到并行数组中。但是我一直在程序末尾崩溃,是否删除不正确?
#include <fstream>
#include <iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
int GetCount(char filename[50]);
int main()
{
char filename[50] = "b.txt";
int choice = 0;
cout << "Input the file name: ";
//cin >> filename;
int numberOfWords = GetCount(filename);
cout << endl << numberOfWords << endl;
ifstream fileRead;
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
fileRead.open(filename);
if (!fileRead.is_open())
{
std::cerr << "\n Error opening file\n";
}
else
{
//Buffers to get words from file
char buffer[1000];
char bufferBuffer[1000];
我在这里声明导致删除问题的数组
//Array of pointers to words
char ** cStringArray = new char *[numberOfWords];
*cStringArray = nullptr;
int add = 1; //Integer in case a word is repeated not to add
//Parrallel Array to integers
int * intPtr = new int;
*intPtr = 0;
//Pointers used to check for repeated words
int * startIntPtr = intPtr;
int * checkIntPtr = startIntPtr;
char ** startCStringArray = cStringArray;
char ** checkCStringArray = cStringArray;
//Index of Current Word
int indexOfWord = 0;
//Variables for character check
int i = 0;
int k = 0;
//Loop to get words from file
while (fileRead >> buffer)
{
//Add Word
add = 1;
//Copy only alpha characters
i = 0;
k = 0;
while (buffer[k] != '\0')
{
if (isalpha(buffer[k]))
{
bufferBuffer[i] = buffer[k];
i++;
}
k++;
}
bufferBuffer[i] = '\0';
strcpy(buffer, bufferBuffer);
//Actually set it in array
*cStringArray = new char[strlen(buffer) + 1];
strcpy(*cStringArray, buffer);
//Compare Against all other cstrings
for (int ii = 0; ii < indexOfWord; ii++)
{
if (strcmp(*checkCStringArray, buffer) == 0)
{
*checkIntPtr += 1;
add = 0;
}
checkCStringArray++;
checkIntPtr++;
}
//Reset Checks
checkIntPtr = startIntPtr;
checkCStringArray = startCStringArray;
//Move onto next space of cStringArray
if (add == 1)
{
cStringArray++;
*cStringArray = nullptr;
//Add one to index
indexOfWord += 1;
//Add one to intPtr
*intPtr += 1;
intPtr++;
*intPtr = 0;
}
else
{
*cStringArray = nullptr;
*intPtr = 0;
}
}
cStringArray = startCStringArray;
for (int i = 0; i < indexOfWord; i++)
{
cout << *cStringArray << endl;
cStringArray++;
}
cStringArray = startCStringArray;
intPtr = startIntPtr;
for (int i = 0; i < indexOfWord + 1; i++)
{
cStringArray[i] = nullptr;
delete[] cStringArray[i];
}
cStringArray = startCStringArray;
在此行中断
delete [] cStringArray;
/*delete[] intPtr;*/
}
return 0;
}
每个new
必须与delete
配对。 让我们检查一下,好吗?
char ** cStringArray = new char *[numberOfWords];
与
delete [] cStringArray;
Groovy的。
*cStringArray = new char[strlen(buffer) + 1];
与
delete[] cStringArray[i];
看起来都不错
除了...
让我们看一下最后一个带有更多上下文的内容。
for (int i = 0; i < indexOfWord + 1; i++)
{
cStringArray[i] = nullptr;
delete[] cStringArray[i];
}
Hmmmm。 将指针设置为nullptr
。 删除nullptr
。 程序在删除指针之前丢失了指针并泄漏了内存。
仔细观察并减少周围的噪音不太好。
请注意,这只是查看删除内容。 我没有看过的代码中可能还有其他问题。 乍一看,它似乎过于复杂。 这就是最小,完整和可验证示例如此有用的原因。 如果您将所有不必要的东西都切掉了,那么错误会更加突出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.