【问题标题】:C programmin: Segmentation fault (core dumped) [closed]C 编程:分段错误(核心转储)[关闭]
【发布时间】:2016-05-26 17:10:34
【问题描述】:

我试图在 C 编程中用辛普森的方法做这个积分计算器,但它出现了一个错误:分段错误(核心转储)。我用谷歌搜索了一些关于它的东西并尝试了 gdb 调试器,但我什么也没得到。

gdb 声称: 程序收到信号 SIGSEGV,分段错误。 0x00000000004008f6 in main()

我不是专业的程序员,我不懂指针之类的东西。

这个错误是什么意思?我该如何解决?应该注意什么才能防止它发生/发现错误?

提前致谢(:

#include<stdio.h>
#include<math.h>

double inta,suma; //value of the integral and a provisory value for it
int a; //a sort of boolean

double expo(double x) //the function to be integrated
{
    return exp(-0.5 * pow(x,2));
}


double integration(double min, double max, int division) //the process of integration
{  
    double step = (max - min)/(pow(2,division)); //define the number of steps
    double fake_a;  
    fake_a = min; //a value thats equal to the minimum
    int counter; // to count how many times the steps have been taken
    counter = 1;
    while(fake_a < max)
    {
        fake_a = fake_a + counter*step; //the first value of a
        if(counter % 2 == 0) //even
        {
            suma = suma + 2*expo(fake_a);
            counter++;
        } 
        else //odd
        {
            suma = suma + 4*expo(fake_a);
            counter++;
        }
    }

    inta = suma + expo(min) + expo(max); //final value of the integral
    counter = 1; //reset counter
    return inta;
}

int main()
{
    int N; //N of columns of matrix_inte
    double matrix_inte[N][2]; //a matrix for the evaluating the integrals
    a = 0; //boolean
    double acc = pow(10,-6); //accuracy
    N = 2; //length of the integral vector
    int n_div; //number of divisions
    double mn = -1.0;
    double mx = 1.0;
    n_div = 10;
    //first values of the matrix
    matrix_inte[1][1] = 0; 
    matrix_inte[2][1]= 1;
    matrix_inte[1][2] = 0;

    while(a=0) //main loop
    {
        for(int i=2;i<=N;i=i+1) //takes the differences of a vector[i] and vecotr[i-1]
        {
            double result;
            result = fabs(matrix_inte[i][1]-matrix_inte[i-1][1]);
            matrix_inte[i][2] = result;
        }

        for(int j = 1;j <= N; j = j + 1)
        { 
            if(matrix_inte[j][2] > acc) //checks if there is any value greater than acc
            {
                n_div = n_div + 10;
                N = N + 1;
                inta = 0;
                suma = 0; //readjusts the variables
                integration(mn,mx,n_div);
                matrix_inte[N][1] = inta; //recalculate the integral
            }                    
            else
            {
                printf("%f ", matrix_inte[j][1]); //shows the value of the integral
                a = 1; //changes the boolean
            }
        }
    }
    return 0;
}

【问题讨论】:

  • 你介意创建一个MCVE吗?
  • 调试器会很容易地告诉您崩溃发生的准确位置。它可能是您访问包含 2 个元素的数组的第三个元素的地方,甚至在循环之前。
  • ...并且N 在您使用double matrix_inte[N][2]; 时未初始化。
  • int N; double matrix_inte[N][2]; : N 未初始化。
  • while(a=0) -> while(a == 0)

标签: c pointers ubuntu segmentation-fault


【解决方案1】:

如何读取编译器消息?

while(a = 0){ … }

是对您的循环没有影响的语句,因为它将a 分配为具有0 的值,这不是您循环的条件检查。您可能应该使用过相等比较运算符 while(a == 0){ … }
更进一步,2 超出了数组 double matrix_inte[N][2] 的末尾。

matrix_inte[1][2] = 0; 

这里也是如此,但 N 从未初始化。

matrix_inte[i][2] = result;

希望这能有所帮助。

【讨论】:

  • 虽然我明白,但这是我的错误输入。我不明白其他人。为什么 2 超过数组的末尾?它有 N 行和 2 列,所以我认为 matrix_inte[1][2] 表示第一行和第二列。为什么 N 从未初始化?正如我所说,我是编程新手,甚至是 C 编程的“新人”,我还不太了解。
  • 抱歉回复晚了。如果数组有两列,则它们的索引为 [0] 和 [1]。用 [2] 索引的元素已经是“第三列”。可变长度数组在 C11 中是可选的,因此它可能更安全int N = 2; double matrix_inte[N][2]; 并在编译器中打开所有警告。
猜你喜欢
  • 2021-10-06
  • 2013-04-28
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多