当数据类型和返回类型从int更改为float或double时,行列式函数中的分段错误

问题描述 投票:0回答:2

当我运行它时,我收到“分段错误”错误!我正在尝试使用hilbert()函数数组输出作为另一个名为determinant()的函数的输入。我尝试使用静态,动态数组等等。这是我的代码:(我必须创建一个希尔伯特矩阵并使用该矩阵作为行列式函数的输入来找到它的行列式)

我试图找到一个4乘4希尔伯特矩阵,然后通过行列式函数运行它,该函数包含另一个3×3矩阵的deter()函数。请帮忙。


#include<stdio.h>
#include<stdlib.h>
 double arr3[4][4];

 double deter(double m[3][3])
{

 double determinant= m[0][0] * ((m[1][1]*m[2][2]) - (m[2][1]*m[1][2])) -m[0][1] * (m[1][0]
   * m[2][2] - m[2][0] * m[1][2]) + m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]);



  return determinant;
}

double determinant(double **a,int o)  //  o denotes order of the matrix
{
    int i,j;              
    double b[3][3];
    int m,n;   
    int c,s=1;    // s is for the signed values; c is used for expanding along the row
    double det;


        det=0;   
        for(c=0;c<=o-1;c++)  // c used for iterating along the row 
        {
            m=0,n=0;
            for(i=0;i<o;i++)
            {
                for(j=0;j<o;j++)
                {
                    b[i][j]=0; // array b initialized zero      

                    if(i!=0 && j!=c)  // For obtaining the matrix of minor by deleting the first row and the concerned element column
                    {
                        b[m][n]=a[i][j];  
                        if(n<(o-2))   //to allow elements of the minor to be stored, we need to increment m and n as well.
                            n++;
                        else
                            {
                                n=0; // restarting the n values for columns 
                                m++; // increment m for next row values
                            }
                    }
                }
            }

            det=det+ s * (a[0][c]*deter(b)); // The main recursive determinant function; a[0][c] denotes the expanding along the row approach; next recursion to find determinant of the lesser order minor
            s=-1*s;  // to get cofactor from minor 
        }


    return(det);
}



double **hilbert()
{

    //int m=4;
  double **array;
    array=malloc(sizeof(int*) * 4);
    for(int i = 0 ; i < 4 ; i++)
        {  array[i] = malloc( sizeof(int*) * 4);
        }

    for(int i=0;i<4;i++)
    {

        for(int j=0;j<4;j++)
        {
           array[i][j] = 1.0/((i + 1) + (j + 1) - 1.0);
        }
    }

 /*printf("Hilbert actual is \n");   
     for(int i=0;i<4;i++)
    {
        printf("\n");
        for(int j=0;j<4;j++)
        {
            printf("%lf\t",array[i][j]);
        }
    }
*/
 return array;   
}


 int main()
{
   //double a[4][4];
    int i,j;
    double d;
   // hilbert();
   // double **ab;

      double **aff=hilbert();

/*    printf("\nEnter the matrix elements: ");
    for(i=0;i<=3;i++)
    {
        for(j=0;j<=3;j++)
        {
            scanf("%d",&a[i][j]);  //taking the input
        }
    }
    // d=determinant(a,3);         //calling the determinant function
    //printf("\nDeterminant is %d",d);
  */ 
  printf("\nHilbert matrix is : \n");
  for(i=0;i<=3;i++)
    {   
        printf("\n");
        for(j=0;j<=3;j++)
        {
            printf("%lf\t",aff[i][j]);  //taking the input
        }
    }

   d=determinant(aff,4);  //determinant function
  printf("\nDeterminant is %lf",d);
  free(aff);
    return 0;
}

c arrays pointers multidimensional-array segmentation-fault
2个回答
0
投票

您的代码会在此行导致越界写入:

 array[i][j] = 1.0/((i + 1) + (j + 1) - 1.0);

以下是运行时错误,这是一个live test of your code

===========开始#0 stensal运行时消息===========

运行时错误:[越界写入] 继续执行会导致未定义的行为,中止!

-
- Writing 8 bytes to 0x872f050 will corrupt the adjacent data.
- 
- The memory-space-to-be-written (start:0x872f040, size:16 bytes) is allocated at
-     file:/prog.c::66, 23
- 
-  0x872f040               0x872f04f
-  +------------------------------+
-  |the memory-space-to-be-written|......
-  +------------------------------+
-                                  ^~~~~~~~~~
-        the write starts at 0x872f050 that is right after the memory end.
- 
- Stack trace (most recent call first) of the write.
- [1]  file:/prog.c::74, 12
- [2]  file:/prog.c::100, 20
- [3]  [libc-start-main]
-

============#0 stensal运行时消息结束============


0
投票

首先,建议阅读:

Correctly allocating multi-dimensional arrays

然后,让我们看看hilbert()

double **hilbert()
{
    // ...
    double **array;
//  ^^^^^^ 
    array = malloc(sizeof(int*) * 4);          //  -->  malloc(sizeof *array * 4);
//                       ^^^^^
    for(int i = 0 ; i < 4 ; i++)
    {
        array[i] = malloc( sizeof(int*) * 4);  //  -->  malloc(sizeof **array * 4)
//                               ^^^^^^    
    }
// ...
}

另见answerstensal,其中发现了越界通行证。

© www.soinside.com 2019 - 2024. All rights reserved.