繁体   English   中英

VC ++运行时错误:调试声明失败

[英]VC++ Runtime Error : Debug Assertation Failed

目前,我正在获得运行时“断言错误”

这是错误:

在此处输入图片说明

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

我知道问题是由这段代码引起的,有关我的逻辑的某些信息已关闭,只是看不到它是什么。

  //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++;
            }
        }
    }

另外,如何将这个动态分配的2D数组传递给函数? (确实需要对代码进行整体清理)

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];
}

这是代码的全部范围。.非常感谢您的帮助!

这是正在读取的内容: 在此处输入图片说明

和当前的输出(输出就是假设的样子): 在此处输入图片说明

#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>保持单词频率。 如果将条目插入地图,地图将自动在一个条目中存储相似的单词(假设您从单词中删除了垃圾),并且该计数将自动增加到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::mapremove_if()等。

然后打印出来只是遍历地图并打印每个条目(字符串和计数)。 如果添加打印,则可能是4行额外的代码。 因此,实际上,几乎所有的分配工作都有效地执行了大约10行代码。

删除以下代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM