【问题标题】:How can I trace back the error如何追溯错误
【发布时间】:2016-03-01 02:53:50
【问题描述】:

我被分配创建一个数组检查(查看数组是增加、减少还是两者都没有[然后退出,如果两者都没有])和对我的一项任务进行递归二进制搜索。在同行的帮助下,我能够做到这些事情,但我需要帮助来找出似乎导致错误的原因

在抛出 'std::logic_error' 的实例后调用终止 what(): basic_string::_S_construct null 无效 中止

运行代码时。我用谷歌搜索了这个错误,这个错误似乎很模糊,或者我只是不明白。它编译没有错误,但我需要帮助来找出我做错了什么。它能够在没有 binarySearchR 函数及其关联代码的情况下运行,因为数组检查本身就是先前的分配。以下是代码,在此先感谢您!

#include <iosteam>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

int checkArraySort (string *fileLines, int numberOfLines);
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax);

int main ()
{
int numberOfLines = 0;
    string searchKey = 0;

    cout << "Input search key: ";
    cin >> searchKey;

        ifstream fileIn;
        fileIn.open("words_in.txt");

        string line;

        if (fileIn.eof()) /* Checks file to see if it is blank before proceeding */
        {
                exit (EXIT_SUCCESS);
        }

    else
    {
        while(!(fileIn.eof()))
        {
                    fileIn >> line;
                    numberOfLines++;
      }

            fileIn.close(); /* closes fileIn, need to reopen to reset the line location */
            fileIn.open("words_in.txt");

            string *fileInLines;
            fileInLines = new string[numberOfLines];

            for (int i = 0; i < numberOfLines; i++)
            {
                    fileIn >> line;
                    fileInLines[i] = line;
            }

            fileIn.close(); /* closes fileIn */

        int resultingCheck = checkArraySort(fileInLines, numberOfLines);

        if (resultingCheck == -1)
        {
            cout << "The array is sorted in descending order." << endl;
        }

        else if (resultingCheck == 1)
        {
            cout << "The array is sorted in ascending order." << endl;
        }

        else
        {
            cerr << "ERROR: Array not sorted!" << endl;
            exit (EXIT_FAILURE);
        }

        int searchResult = binarySearchR (fileInLines, searchKey, 0, numberOfLines);

        if (!searchResult == -1)
        {
            cout << "Key found at index " << searchResult << "." << endl;
        }

        else
        {
            cout << "Key not found at any index." << endl;
        }

            exit (EXIT_SUCCESS);
    }
}

int checkArraySort (string *fileLines, int numberOfLines)
{
    int result = 1; /* Ascending by default */

    for (int i = 1; i < numberOfLines; i++) /* Checks if decending */
    {
        if (fileLines[i] < fileLines[i-1])
        {
            result = -1;
        }
    }

    if (result == -1) /* Makes sure it is descending (or if it is neither) */
    {
        for (int i = 1; i < numberOfLines; i++)
        {
            if (fileLines[i] > fileLines[i-1])
            {
                result = 0;
            }
        }
    }

    return result;
}

int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax)
{
    // so, its gotta look at the center value and each times, it discards half of the remaining list.

    if (iMax < iMin) /* If the minimum is greater than the maximum */
    {
        return -1;
    }

    else
    {
        int iMid = (iMin + iMax) / 2;

        if (fileLines[iMid] > searchKey) /* If the key is in the lower subset */
        {
            return binarySearchR (fileLines, searchKey, iMin, iMid - 1);
        }

        else if (fileLines[iMid] < searchKey) /*If the key is in the upper subset */
        {
            return binarySearchR (fileLines, searchKey, iMin, iMid + 1);
        }

        else /*If anything else besides the two */
        {
            return iMid;
        }
    }
}

【问题讨论】:

  • 此错误通常表示内存损坏/未定义的行为。原因可能是任何事情。使用调试器找出异常被抛出的位置。您的代码中至少存在一个错误:“while(!(fileIn.eof()))”is always a bug.
  • 另外:当数组升序或降序时,您将执行二进制搜索,但您的二进制搜索仅在数组按升序时有效。此外,您在二分搜索中的第二次递归调用是错误的。此代码存在多个问题。

标签: c++ debugging logic stdstring terminate


【解决方案1】:

简单的方法:添加一堆 cout 以查看您的程序去向以及值是什么。

优点

  • 简单易做

缺点

  • 每次想要添加更多信息时都需要重新编译

艰难之路:学习使用调试器

优点

  • 可以“即时”检查
  • 不需要重建
  • 可以使用您在其他所有 C++ 程序中学到的知识

缺点

  • 需要进行一些研究才能了解如何操作。

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2017-07-05
    • 2019-11-15
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2023-03-14
    相关资源
    最近更新 更多