gpt4 book ai didi

c++ - VC++ 运行时错误 : Debug Assertation Failed

转载 作者:行者123 更新时间:2023-11-28 00:30:04 27 4
gpt4 key购买 nike

目前我遇到运行时“断言错误”

这里是错误:

enter image description here

我正在将文本文件中的单词读入动态分配的数组中。这段代码是我填充新数组的地方。

我知道问题是由这段代码引起的,我的逻辑有些不对劲,只是看不出是什么。

  //fill new arrays
for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i];
New_WordCount[y] = WordCount[i];
y++;
}
}
}

另外,我如何将这个动态分配的二维数组传递给函数? (代码确实需要整体清理一下)

char** SentenceArry = new char*[NUM_WORDS]; //declare pointer for the sentence
for( int i = 0; i < NUM_WORDS; i++)
{
SentenceArry[i] = new char[WORD_LENGTH];
}

这是代码的全部内容。非常感谢您的帮助!

这是正在读取的内容:enter image description here

和当前输出(输出应该是这样的):enter image description here

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <iomanip>

using std::setw;
using std::left;

using std::cout;
using std::cin;
using std::endl;

using std::ifstream;


int main()
{

const int NUM_WORDS = 17;//constant for the elements of arrays
const int WORD_LENGTH = 50;//constant for the length of the cstrings (NEED TO GIVE THE VALUE ZERO STILL!)
short word_entry = 0; //declare counter
short new_numwords= 0; //declare new word count
char EMPTY[1][4]; //NULL ARRAY
EMPTY[0][0] = '\0';//define it as null


char** SentenceArry = new char*[NUM_WORDS]; //declare pointer for the sentence
for( int i = 0; i < NUM_WORDS; i++)
{
SentenceArry[i] = new char[WORD_LENGTH];
}

int WordCount[NUM_WORDS];//declare integer array for the word counter

for(int i = 0; i < NUM_WORDS; i++)//fill int array
{
WordCount[i] = 1;
}

int New_WordCount[NUM_WORDS] = {0};

ifstream read_text("DataFile.txt"); //read in our text file

if (read_text.is_open()) //check if the the file was opened
{
read_text >> SentenceArry[word_entry];

//REMOVE PUNCTUATION BEFORE BEING READ INTO THE ARRAY
while (!read_text.eof())
{

word_entry++; //increment counter
read_text >> SentenceArry[word_entry]; //read in single words of the text file into the array SentenceArry

char* ptr_ch;//declare our pointer that will find chars

ptr_ch = strstr( SentenceArry[word_entry], ",");//look for "," within the array

if (ptr_ch != NULL)//if true replace it with a null character
{
strncpy( ptr_ch, "\0" , 1);
}//end if
else
{

ptr_ch = strstr( SentenceArry[word_entry], ".");//look for "." within the array

if (ptr_ch != NULL)//if true replace it with a null character
{
strncpy( ptr_ch, "\0" , 1);
}//end if
}//end else
} //end while
}//end if

else
{
cout << "The file could not be opened!" << endl;//display error message if file doesn't open
}//end else

read_text.close(); //close the text file after eof



//WORD COUNT NESTED FOR LOOP
for(int y = 0; y < NUM_WORDS; y++)
{
for(int i = y+1; i < NUM_WORDS; i++)
{
if (strcmp(SentenceArry[y], EMPTY[0]) == 0)//check if the arrays match
{
y++;

}
else
{
if (strcmp(SentenceArry[y], SentenceArry[i]) == 0)//check if the arrays match
{
WordCount[y]++;
strncpy(SentenceArry[i], "\0" , 3);
}//end if
}//end if
}//end for
}//end for


//find how many arrays still contain chars
for(int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
new_numwords++;
}
}


//new dynamic array
char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[new_numwords];
}



//fill new arrays
for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i];
New_WordCount[y] = WordCount[i];
y++;
}
}
}

//DISPLAY REPORT
cout << left << setw(15) << "Words" << left << setw(9) << "Frequency" << endl;
for(int i = 0; i < new_numwords; i++) //compare i to the array constant NUM_WORDS
{
cout << left << setw(15) << New_SentenceArry[i] << left << setw(9) << New_WordCount[i] << endl; //display the contents of the array SentenceArry
}


//DEALLOCATION
for( int i = 0; i < NUM_WORDS; i++)//deallocate the words inside the arrays
{
delete [] SentenceArry[i];
}

for(int i = 0; i < new_numwords; i++)
{
delete [] New_SentenceArry[i];
}

delete [] SentenceArry; //deallocate the memory allocation made for the array SentenceArry
delete [] New_SentenceArry;//deallocate the memory allocation made for the array New_SentenceArry

}//end main

最佳答案

代码有几个问题,尽管这可以使用 C++ 编写,而不是带有少量 C++ I/O 的 C..

Issue 1:

由于您使用的是 C 风格的字符串,因此任何字符串数据的复制都需要函数调用,例如 strcpy()、strncpy() 等。您未能在这段代码中遵循以下建议:

for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i]; // This is wrong
New_WordCount[y] = WordCount[i];
y++;
}
}
}

你应该使用 strcpy(),而不是 =复制字符串。

strcpy(New_SentenceArry[y], SentenceArry[i]);

Issue 2:

您应该为原始数组和新数组分配 WORD_LENGTH。字符串的长度与字符串的数量无关。

char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[new_numwords];
}

这应该是:

char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[WORD_LENGTH];
}

Issue 3:

您的循环不会检查索引是否超出数组范围。

您似乎是根据当前使用的数据编写程序,而不是不管数据是什么都编写代码。如果您将自己限制在 17 个单词以内,检查索引是否超过 16 的方法在哪里?无处。

例如:

while (!read_text.eof() )

应该是:

while (!read_text.eof() && word_entry < NUM_WORDS) 

Issue 4:

您没有处理正确找到的第一个字符串:

read_text >> SentenceArry[word_entry];  // Here you read in the first word
while (!read_text.eof() )
{
word_entry++; //increment counter
read_text >> SentenceArry[word_entry]; // What about the first word you read in?

Summary:
即使进行了这些更改,我也不能保证程序不会崩溃。即使它不会因这些更改而崩溃,我也不能保证它会在 100% 的时间内正常工作——保证需要进一步分析。

鉴于此作业的内容,正确的 C++ 解决方案是使用 std::map<std::string, int>保持词频。 map 会自动将相似的单词存储在一个条目中(假设您从单词中删除了垃圾),并且会在将条目插入 map 时自动将计数增加到 1。

像这样:

#include <string>
#include <map>
#include <algorithm>

typedef std::map<std::string, int> StringMap;
using namespace std;

bool isCharacterGarbage(char ch)
{ return ch == ',' || ch == '.'; }

int main()
{
StringMap sentenceMap;
//...
std::string temp;
read_text >> temp;
temp.erase(std::remove_if(temp.begin(), temp.end(), isCharacterGarbage),temp.end());
sentenceMap[temp]++;
//...
}

仅该代码就完成了原始代码所做的一切——跟踪字符串、增加字数、在处理之前从字中删除垃圾字符等。但最重要的是,没有手动内存管理。没有调用 new[]、delete[],什么都没有。代码只是“有效”。这实际上是5 行 代码,您只需要编写一个“读取”循环。

我不会详细介绍每个细节,您可以自己完成,因为代码很小,并且有大量资源可以解释 std::map , remove_if()

然后打印出来只是遍历 map 并打印每个条目(字符串和计数)。如果您添加打印,那可能是 4 行额外的代码。所以总的来说,实际上所有的作业都是用 10 行左右的代码有效完成的。

关于c++ - VC++ 运行时错误 : Debug Assertation Failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23288863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com