简体   繁体   English

传递多维数组时出现分段错误,C ++

[英]Segmentation fault while passing multidimensional arrays, c++

I'm pretty new to coding in c++ so I apologize if this question has a very apparent simple answer. 我对使用C ++进行编码非常陌生,因此如果这个问题有一个非常明显的简单答案,我深表歉意。 I'm trying to create a matrix of random numbers from -1 to 1. These are the two functions I am using: 我正在尝试创建一个从-1到1的随机数矩阵。这是我正在使用的两个函数:

#include <iostream>
#include "matrix_fill_random.cpp"
using namespace std;

int main(int argc, int argv[]){

int n1, n2, n3;

if (argc != 4) {
    cerr << "This program requires 3 argument!" <<endl;
    return 1;
}
else{
    n1 = argv[1];
    n2 = argv[2];
    n3 = argv[3];

    double** a;

    matrix_fill_random(n1, n2, a);

    return 0;
}
}

and

#include <iostream>
using namespace std;

int matrix_fill_random(int n1, int n2, double** a){
for (int i=0; i<n1; i++){
    for (int j=0; j<n2; j++){
        double num = rand() % 2 - 1;
        a[i][j]=num;
    }
}
return 0;
}

Ultimately I'm trying to create two matrices and then multiply them together so n1, n2, and n3 represent the rows and columns of two matrices, but that isn't all too important right now. 最终,我尝试创建两个矩阵,然后将它们相乘,以便n1,n2和n3代表两个矩阵的行和列,但是现在这并不是太重要。 I think the error might be in how I declare my variables or pass them to other functions but I'm not entirely sure. 我认为错误可能出在我如何声明变量或将其传递给其他函数的问题上,但我不确定。

I feel like if I can understand the principle of creating one of the matrices then that would translate to the other functions I need to use. 我觉得如果我能理解创建矩阵之一的原理,那么它将转化为我需要使用的其他函数。

double** a;

You haven't allocated memory for the pointer, so you're getting Undefined Behavior each time you dereference it using operator [] . 您尚未为指针分配内存,因此,每次使用operator []取消引用它时,都会得到Undefined Behavior。

You should allocate a once before passing it through the function... 您应该分配a将其通过该功能前一次...

double** a = new double*[n1];

and once more inside the for loop of the function: 并再次在该函数的for循环内:

for (int i = 0; i < n1; i++)
{
    a[i] = new double[n2];
    for (int j = 0; j < n2; j++)
    {
        double num = rand() % 2 - 1;
        a[i][j] = num;
    }
}

But don't forget to delete[] the pointer once you're done using it. 但是,一旦使用完毕,不要忘记delete[]指针。 You should delete the other pointers allocated inside the for loop as well. 您还应该delete在for循环内分配的其他指针。

Of course, this can all be avoided by using std::vector . 当然,这可以通过使用std::vector来避免。 Here is your program fitted with the Standard Library: 这是装有标准库的程序:

std::vector<std::vector<double>> a(n1, std::vector<double>(n2));

int matrix_fill_random(std::vector<std::vector<double>> a)
{
    for (int i = 0; i < a.size(); ++i)
    {
        for (int j = 0; j < a[i].size(); ++j)
        {
            double num = rand() % 2 - 1;
            a[i][j] = num;
        }
    }
    return 0;
}

Allocate the memory for a . 分配的内存a You haven't allocate the memory for a. 您尚未为a分配内存。 Chane the statement double** a; 将语句改为double** a; to

  double** a = new double[n1];

and change the loop as follows 并如下更改循环

  for (int i = 0; i < n1; i++)
  {
    //Every row will be of size = number of columns.
    a[i] = new double[n2];
    for (int j = 0; j < n2; j++)
    {
        double num = rand() % 2 - 1;
        a[i][j] = num;
    }
  } 
double** a;
matrix_fill_random(n1, n2, a);

passes uninitialized pointer a to the function, which tries to initialize elements of two-dimensional array: 将未初始化的指针a传递给该函数,该函数尝试初始化二维数组的元素:

a[i][j]=num;

which invokes an undefined behavior . 这将引发未定义的行为 The simplest solution would be to allocate a single block of memory that will be big enough to hold the matrix: 最简单的解决方案是分配一个足以容纳矩阵的内存块:

double* a = new double[rows * cols];
matrix_fill_random(n1, n2, a);
delete[] a;
a = NULL;

...

// accessing element a[i][j] in the function's body:
a[i*cols + j] = num;

but most reasonable solution would be using std::vector instead of C-style arrays. 但最合理的解决方案是使用std::vector而不是C样式的数组。

You need to allocate memory to a before passing it to matrix_fill_random() . 您需要先将内存分配给a然后再将其传递给matrix_fill_random()

double** a = new double[n1];

Since you're using C++ though, you should consider using a vector or other template container . 由于您使用的是C ++,因此应该考虑使用vector或其他模板容器

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

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