所以在课堂上,我们必须创建一个代码,它从文件中获取单词,忽略标点符号,并打印出单词。不仅如此,它还必须忽略相同单词的副本,而且要跟踪哪些单词重复,并将其计数添加到并行array.But中。我一直在程序末尾崩溃,我删除得不正确吗?
#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;
}
发布于 2018-01-26 04:51:20
每个new
都必须与delete
配对。让我们检查一下,好吗?
char ** cStringArray = new char *[numberOfWords];
配对与
delete [] cStringArray;
真棒。
*cStringArray = new char[strlen(buffer) + 1];
配对与
delete[] cStringArray[i];
这一切看起来都不错。
除了..。
让我们用更多的上下文来看一下最后一个。
for (int i = 0; i < indexOfWord + 1; i++)
{
cStringArray[i] = nullptr;
delete[] cStringArray[i];
}
嗯。将指针设置为nullptr
。删除nullptr
。在删除指针之前,程序丢失了指针并泄漏了内存。
如果仔细观察,周围的噪音更小,情况就不太好了。
请注意,这只是删除。代码中可能还有其他我没有看过的问题。乍一看,它似乎过于复杂。这就是Minimal, Complete, and Verifiable example如此有用的原因。如果你去掉所有不必要的东西,它会让错误变得更突出。
https://stackoverflow.com/questions/48455813
复制