【问题标题】:VC++ Runtime Error : Debug Assertation FailedVC++ 运行时错误:调试断言失败
【发布时间】:2014-04-25 09:12:07
【问题描述】:

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

这是错误:

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

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

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

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

这是正在阅读的内容:

和当前的输出(输出是它的样子):

#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

【问题讨论】:

  • 还没有查看完整的代码,但是将某些内容与 strcmp 进行比较,然后将其分配为 = 是第一个奇怪的事情。猜想:您假设内容被复制,然后您将其删除 [] 两次(源和复制)? ... std::string, 矢量 ...
  • @Dyluckk - 你为什么不使用 std::vector 而不是那些动态数组,而只是简单的旧 std::string 而不是那些 new char[] 语句?然后,如果不是所有问题,大多数问题都会消失。现在,你所做的只是用很少的 C++ 编写“C”代码。
  • @PaulMcKenzie 我应该澄清一下,这是我学校的作业,重点是使用指针和 cstrings 动态分配的数组。相信我,我很乐意只使用字符串类型哈哈。
  • 很可能你试图delete 一个已经删除的指针两次。为了避免在删除它们后设置指向NULL 的指针。
  • @Dyluckk - 现在你明白为什么 C++ 程序员不写这样的程序了。即使身后有SO,如果不不断运行和调试,也很难找到问题所在。如果没有像 Valgrind 这样的工具,解决像你这样的问题会占用志愿者的时间。问题可能是一个错误。但要弄清楚需要分析。这个问题的解决方案 - 永远不要这样写代码!”。时间就是金钱。不仅如此,这个练习还让正在学习 C++ 的学生关闭 . 所以告诉你的老师,“干得好——你创造了新的 Java 程序员”。

标签: c++ debugging pointers runtime-error dynamic-memory-allocation


【解决方案1】:

代码有几个问题,尽管这可以使用 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&lt;std::string, int&gt; 来保持词频。该地图会自动将相似的单词存储在一个条目中(假设您从单词中删除了垃圾),并在将条目插入地图时自动将计数增加到 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 行左右的代码完成的。

【讨论】:

  • 谢谢,“问题 1”是问题的很大一部分。 “问题 2”是导致释放引发错误的原因。使用您建议的方法显然是更好的选择。也许我会开始使用实际的“C++”发送她的程序,以指出这些程序是多么不符合她想要的方式。 ;) 再次感谢!
【解决方案2】:

删除下面的代码。

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

【讨论】:

  • 我不会因此而发生内存泄漏吗?
猜你喜欢
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多