【问题标题】:Unknown C++ error: Fatal error: glibc detected an invalid stdio handle未知的 C++ 错误:致命错误:glibc 检测到无效的 stdio 句柄
【发布时间】:2019-11-19 06:56:22
【问题描述】:

我一直在整个互联网上搜索以试图了解为什么会出现此错误,但我只见过这种错误出现在我不熟悉的其他编程语言或我以前从未使用过的代码中。

我正在为学校作业开发一个快速排序程序,一切进展顺利,直到我决定将中值冒泡排序算法整合到一个辅助函数中。

int* QS::MedianSort(int left, int middle, int right) {
    cout << "starting MedianSort...\n";////////////
    int temp;
    static int sort [] = { left, middle, right };
    do {
            if (sort[0] <= sort[1] && sort[1] <= sort[2]) {
                break;
            }
            else if (sort[1] < sort[0]) {
                temp = sort[1];
                sort[1] = sort[0];
                sort[0] = temp;
            }
            else if (sort[2] < sort[1]) {
                temp = sort[2];
                sort[2] = sort[1];
                sort[1] = temp;
            }

    } while(true);

    cout << "...returning sort from Median Sort\n";//////////
    return sort;
}

它在我的分区算法中的实现示例:

        int *temp;
        int tempInt;

        temp = MedianSort(*(array + left), *(array + pivotIndex), *(array + right));
        *(array + left) = *(temp + 0);
        *(array + pivotIndex) = *(temp + 1);
        *(array + right) = *(temp + 2);

        tempInt = *(array + pivotIndex); 
        *(array + pivotIndex) = *(array + left);
        *(array + left) = tempInt;

array 是一个指向我正在快速排序的数组的指针,left 是传递给分区函数的最小索引,right 是最大的。

当我编译代码时,它没有问题,但是一旦我运行可执行文件,我就会在控制台中得到这个错误:

Fatal error: glibc detected an invalid stdio handle
Aborted

我不明白为什么,因为它以 5 个输入文件中的第 3 个输入文件结束,但甚至不启动第 4 个文件。 (我知道这一点是因为我在几乎每个函数的开头都有cout 语句,以向我展示它的进展情况。)

此外,main.cpp 已成功浏览了到目前为止的所有文件。

编辑: 我正在使用-g -std=c++11进行编译

【问题讨论】:

  • P.S.我已经为此工作了几个小时,所以我得到了一些睡眠。大约 7 小时后将再次活跃以澄清任何事情。
  • 我在你的代码中没有看到任何文件。
  • @ZDF 文件在 main.cpp 中调用,这是在头定义文件中。该文件的信息仅包含参数和函数调用。
  • 您的问题与文件有关:您应该显示您的main,最好提供一个最小的工作示例。

标签: c++ data-structures error-handling


【解决方案1】:

此外,main.cpp 已成功浏览了到目前为止的所有文件。

你确定吗?此问题通常与文件有关。您应该提供它或文件处理涉及的范围以查看问题所在。

【讨论】:

    【解决方案2】:

    我认为这行有问题:

    static int sort [] = .....
    

    好像我理解正确,静态变量只会被初始化一次(第一次被调用的函数)。

    【讨论】:

    • 虽然正确,但应该不会导致报错
    • 请注意,这可能应该是一条评论。一旦你获得 50 声望,你就可以这样做了。
    • 会这样做。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    相关资源
    最近更新 更多