无法找出C++代码的分段错误

Can't figure out Segmentation Fault for C++ code

本文关键字:分段 错误 代码 C++      更新时间:2023-10-16

我的代码在下面。当我尝试运行 addArray() 函数时,就会出现问题。我对C++完全陌生,所以我不知道分段错误意味着什么。

我也知道可能有更好的方法来初始化和返回 2d 数组,但我正在慢慢弄清楚。

我现在的主要问题是分段错误。我猜这与我访问变量的方式有关?

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;


int c, q, w, row, coll, quit, qq, opt;
int** arr1;
int** arr2;
int** ans;

//Method Prototypes
int menu();
inline int** getArray(int opt);
inline void printArray(int** arr, int height, int width);
void addArray();
void subtractArray();
void multiplyArrays();
void determArray();
void transposeArray();
void inverseArray();
//Prints out the menu for choosing which option to go with
int menu() {
    cout << "Press 1 for Additionn";
    cout << "Press 2 for Subtractionn";
    cout << "Press 3 for Multiplicationn";
    cout << "Press 4 for Determinantn";
    cout << "Press 5 for Transposen";
    cout << "Press 6 for Inversen";
    cout << "Press 0 to quitnn";
    cin >> c;
    return c;
}
//Main method
int main(void) {
    cout << "C++ 2d Matrix Operations Menun";
    c = menu();
    while (c != 0) {
        if (c == 1) {
            addArray();
        } else if (c == 2) {
            subtractArray();
        } else if (c == 3) {
            void multiplyArrays();
        } else if (c == 4) {
            void determArray();
        } else if (c == 5) {
            void transposeArray();
        } else if (c == 6) {
        }

        c = menu();
    }
    cout << "Press Enter to Quit. GOOD BYE";
    cin >> quit;
    return 0;
}
/*
Prints out the specified array. 
It's arguments are the actual array and the height/weight

 */
inline void printArray(int** arr, int height, int width) {
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            std::cout << arr[i][j] << ' ';
        }
        std::cout << std::endl;
    }

}
//Returns an array.
inline int** getArray(int opt) {
    if (opt == 0) {
        cout << "How many rows and columns should be in the array?n";
        cin >> q >> w;
    } else {
        q = 3;
        w = 3;
    }
    int** ary = new int*[q];
    for (int i = 0; i < q; ++i) {
        ary[i] = new int[w];
    }
    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            cout << "What should the value be for item" << row << "," << coll << "n";
            cin >> ary[row][coll];
        }
    }
    return ary;
}

//Adds arrays
void addArray() {
    arr1 = getArray(0);
    int h1 = q;
    int w1 = w;
    arr2 = getArray(0);
    int h2 = q;
    int w2 = w;
    if ((h1 != h2) || (w1 != w2)) {
        cout << "Both arrays must be the same size.";
        return;
    }

    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr1[row][coll] + arr2[row][coll];
        }
    }
    printArray(ans, q, w);
}
//Subtracts Arrays
void subtractArray() {
    arr1 = getArray(0);
    int h1 = q;
    int w1 = w;
    arr2 = getArray(0);
    int h2 = q;
    int w2 = w;
    if ((h1 != h2) || (w1 != w2)) {
        cout << "Both arrays must be the same size.";
        return;
    }

    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr2[row][coll] - arr1[row][coll];
        }
    }
    printArray(ans, q, w);
}
//Calculate the determinate of an array.
void determArray() {
    arr1 = getArray(1);
    printArray(arr1, q, w);
    //There must be a better/more efficient way to do this using loops.
    int determinant = arr1[0][0]*((arr1[1][1] * arr1[2][2]) - (arr1[2][1] * arr1[1][2])) - arr1[0][1]*(arr1[1][0] * arr1[2][2] - arr1[2][0] * arr1[1][2]) + arr1[0][2]*(arr1[1][0] * arr1[2][1] - arr1[2][0] * arr1[1][1]);
    printf("nDeterminant of vector using method 1 is: %dn", determinant);

}

//Transpose an array.
void transposeArray() {
    cout << "IN TRANS";
    arr1 = getArray(0);
    printArray(arr1, 3, 3);
    //Flip the values
    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr1[coll][row];
        }
    }
    cout << "----------" << endl << "The new vector looks like: n";
    printArray(ans, q, w);
}
/*
        Multiply arrays. One option is to just multiply it by a number and the other is to multiply it by another array.

 */
void multiplyArrays() {

    arr1 = getArray(0);
    int h1 = q;
    int w1 = w;

    cout << "Do you wish to multiply the first vector by a number(Enter 1), or by a second vector(Enter 2)?";
    cin >> qq;
    int mu;
    //First Option is to multiply it by a single number
    if (qq == 1) {

        cout << "What number do you wish to multiply the vector by?";
        cin >> mu;

        for (row = 0; row < q; row++) {
            for (coll = 0; coll < w; coll++) {
                ans[row][coll] = arr1[row][coll] * mu;
            }
        }
        printArray(ans, h1, w1);
        //Multiply two arrays
    } else if (qq == 2) {
        arr2 = getArray(0);
        int h2 = q;
        int w2 = w;

        int n1 = h1;
        int n2 = w2;

        int nCommon = n1;
        if (n2 == nCommon) {
            cout << "Amount of columns for vector 1 must match amount of rows for vector 2";
            return;
        }
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                for (int k = 0; k < nCommon; k++) {
                    ans[i][j] += arr1[i][k] * arr2[k][j];
                }
            }
        }

        printArray(ans, n1, n2);
    }
}

您从不为 ans 分配内存。 就像您需要在填充两个输入数组之前为其分配存储一样,您需要为答案分配存储。

当您尝试写入您无权访问的内存时,会生成分段错误。 在这种情况下,由于ans数组未初始化,因此它指向随机内存。当你执行ans[row][coll] = arr2[row][coll] - arr1[row][coll];时,你会得到一个段错误,因为它指向程序空间之外的某个地方ans[row][col]

问题是您尚未为 ans 数组分配内存,但您正在使用以下代码写入它:

for (row = 0; row < q; row++) {
    for (coll = 0; coll < w; coll++) {
        ans[row][coll] = arr2[row][coll] - arr1[row][coll];
    }
}

这就是您遇到分段错误的原因。首先尝试添加一个块来为ans分配内存。