繁体   English   中英

无法理解 c++ 代码中 SIGTRAP 的原因

[英]Can't understand reason of the SIGTRAP in c++ code

我正在写一些 function 来处理矩阵。 我收到了正确的 output:

 __11111111 __11111111 __333333333 __444444444444

但是程序没有停止并返回无效代码

进程结束,退出代码为 -1073741819 (0xC0000005)

我该如何修复错误?

我尝试了调试器 (gdb) 并发现了这一点:

信号 = SIGTRAP

调试器向我提供了文件new_allocator.h (尝试释放 nullptr)

 // __p is not permitted to be a null pointer.
      void
      deallocate(pointer __p, size_type)
      { ::operator delete(__p); }

我的代码

#include <bits/stdc++.h>

using namespace std;

#define EPS 0.000001

int dualMatrix(vector<vector<double>> a) {
    int n = a.size();
    int m = a[0].size();

    for (int col=0, row=0; col<m && row<n; ++col) {
        int sel = row;
        for (int i=row; i<n; ++i)
            if (abs (a[i][col]) > abs(a[sel][col]))
                sel = i;
        if (abs(a[sel][col]) < EPS)
            continue;
        for (int i=col; i<=m; ++i)
            swap(a[sel][i], a[row][i]);

        cout << "__11111111\n";
        for (int i=0; i<n; ++i)
            if (i != row) {
                if (a[row][col] == 0) {
                    cout << "DIVIDER IS ZERO" << endl;
                    return  -1;
                }
                double c = a[i][col] / a[row][col];
                for (int j=col; j<=m; ++j)
                    a[i][j] -= a[row][j] * c;
            }
        ++row;
    }
    int rank = 0;
    for (int i = 0; i < n; ++i) {
        double temp = a[i][i];
        if (temp == 0) {
            cout << "Diagonal element is 0 at the row=" << i << endl;
            rank = i;
            break;
        }
        for (int j = 0; j < m; ++j) {
            a[i][j] /= temp;
        }
    }
    cout << "__333333333\n";
    //printMatrix(a);
    cout << "__444444444444\n";
    return 0;
}

int main() {
    vector<vector<double>> tmp {{1, 2, 3}, {3, 4, 5}};
    dualMatrix(tmp);
    cout << "__END" << endl;
    return 0;
}
int m = a[0].size();

向量的大小为m

 for (int i=col; i<=m; ++i) swap(a[sel][i], a[row][i]);

在这里, i在向量a[sel]a[row]的边界之外。 使用下标运算符访问其范围之外的向量具有未定义的行为。

同样在这里:

 for (int j=col; j<=m; ++j) a[i][j] -= a[row][j] * c;

我该如何修复错误?

不要在其边界之外访问向量。 如果向量的大小为 3,则有效索引为 0、1 和 2。如果大小为 m,则有效索引为 0、...、m - 1

暂无
暂无

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

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