c - C 中迭代次数少的段错误

标签 c gcc segmentation-fault ubuntu-12.04

我正在运行一个代码来计算卡方密度文件的值并将其打印到屏幕和文件;但是我的问题是,即使点数相对较少(例如 100、500 等),我在屏幕输出的末尾也会出现段错误。我编写了一个代码,它使用更多的数字并且运行没有问题。如果您指出错误并指导我进行补救过程,我将不胜感激。当我尝试使用 malloc 时,出现如下不同的错误:

*** glibc detected *** ./Q4: munmap_chunk(): invalid pointer: 0x0a026978 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7653ee2]
/lib/i386-linux-gnu/libc.so.6(+0x765c5)[0xb76545c5]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb7643424]
./Q4[0x80488b9]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75f74d3]
./Q4[0x8048501]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:08 393219     /home/ongun/Desktop/Dropbox/Computational Physics/PHYS443-MT1/Q4
08049000-0804a000 r--p 00000000 08:08 393219     /home/ongun/Desktop/Dropbox/Computational Physics/PHYS443-MT1/Q4
0804a000-0804b000 rw-p 00001000 08:08 393219     /home/ongun/Desktop/Dropbox/Computational Physics/PHYS443-MT1/Q4
0a026000-0a047000 rw-p 00000000 00:00 0          [heap]
b75a2000-b75be000 r-xp 00000000 08:06 131773     /lib/i386-linux-gnu/libgcc_s.so.1
b75be000-b75bf000 r--p 0001b000 08:06 131773     /lib/i386-linux-gnu/libgcc_s.so.1
b75bf000-b75c0000 rw-p 0001c000 08:06 131773     /lib/i386-linux-gnu/libgcc_s.so.1
b75dc000-b75de000 rw-p 00000000 00:00 0 
b75de000-b7782000 r-xp 00000000 08:06 137731     /lib/i386-linux-gnu/libc-2.15.so
b7782000-b7784000 r--p 001a4000 08:06 137731     /lib/i386-linux-gnu/libc-2.15.so
b7784000-b7785000 rw-p 001a6000 08:06 137731     /lib/i386-linux-gnu/libc-2.15.so
b7785000-b7788000 rw-p 00000000 00:00 0 
b7788000-b77b2000 r-xp 00000000 08:06 137726     /lib/i386-linux-gnu/libm-2.15.so
b77b2000-b77b3000 r--p 00029000 08:06 137726     /lib/i386-linux-gnu/libm-2.15.so
b77b3000-b77b4000 rw-p 0002a000 08:06 137726     /lib/i386-linux-gnu/libm-2.15.so
b77ce000-b77d2000 rw-p 00000000 00:00 0 
b77d2000-b77d3000 r-xp 00000000 00:00 0          [vdso]
b77d3000-b77f3000 r-xp 00000000 08:06 137721     /lib/i386-linux-gnu/ld-2.15.so
b77f3000-b77f4000 r--p 0001f000 08:06 137721     /lib/i386-linux-gnu/ld-2.15.so
b77f4000-b77f5000 rw-p 00020000 08:06 137721     /lib/i386-linux-gnu/ld-2.15.so
bfc86000-bfca7000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

我的代码如下:

#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
#define N 100
// We have three function definitions here
// The factorial function decleration and definition are as follows:
long double factorial (long double);
// Now we define it,
long double
factorial(long double n)
{
    //Here s is the free parameter which is increased by one in each step and
    //pro is the initial product and by setting pro to be 0 we also cover the
    //case of zero factorial.
    int s = 1;
    long double pro = 1;
    //Here pro stands for product.
    if (n < 0)
        printf("Factorial is not defined for a negative number \n");
    else {
    while (n >= s) { 
    pro *= s;
    s++;
    }
    return pro;
    }
}
// The Gamma function declaration and definition are as follows:
long double GAMMA_2(int);
long double 
GAMMA_2(int v)
{
    int i = 1;
    long double factor, multiplier =  sqrtl(M_PI);
    if((v % 2) == 0)
    {
        return factorial((long double )((v / 2) - 1));
    }
    else
    {
        factor = (v / 2.0) - i;
        while(v / 2.0 - (i) > 0)
        {

            factor = (v / 2.0 - i); 
            multiplier *= factor;
            i++;
        }
        return multiplier;
    }
}
// The ChisquarePDF declaration and definition are as follows:
long double ChisquarePDF(long double, int);
long double
ChisquarePDF(long double x, int v)
{
    return powl(x, v/2 - 1) / ( powl(2, v/2) * GAMMA_2(v) * expl(x / 2));
}
int main()
{
    //This is the trial line
    //printf("%Lf \n", GAMMA_2(9));
    int i, v = 10;
    //long double * x = malloc(N* sizeof(long double));
    //long double * y = malloc(N* sizeof(long double));
    long double x[N], y[N];
    // The Chisquare function is defined for positive values of x
    FILE * fp;
    fp = fopen("Q4.dat", "w+");
    for(i = 0; i <= N; i++)
    {
        x[i] = i / 10.0;
        y[i] = ChisquarePDF(x[i], v);
        //printf("%Le             %Le \n", x[i], y[i]);
        fprintf(fp, "%Le             %Le \n", x[i], y[i]);
    }
    //for(i = 0; i <= N; i++)
    //{
        //y[i] = ChisquarePDF(x[i], v);
        //printf("%Le             %Le \n", x[i], y[i]);
        //fprintf(fp, "%Le             %Le \n", x[i], y[i]);
    //}

    fclose(fp);
    //free((void *)x);
    //free((void *)y);
    return 0;
}

最佳答案

main你的for循环直到 i等于 N因此读写x[N]y[N]它刚好超出分配的数组。 (对于声明为 N : int array[N]; 的数组,您可以访问从 array[0] 到(包括) array[N-1] 的元素。所以 array[N] 正好超出了这个范围)。将循环条件更改为:

for(i = 0; i < N; i++)

此外,我收到关于 factorial 的警告它到达结尾但没有返回有效值 ( warning: control reaches end of non-void function )。即使未为负数定义阶乘(如 if(n < 0) 内),您也必须返回一个值:

if(n < 0){
    printf("Factorial is not defined for a negative number \n");
    return 0;
}else{
    ...

关于c - C 中迭代次数少的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272835/

相关文章:

带有 C 库的 Python

c - 在枚举中包含 .csv 如何工作?

c - 我怎样才能为 Windows 制作一个二进制文件,使非编码人员能够只获得一个可以提供给 gdb 的故障转储?

c++ - Qt OpenCV - 显示转换帧时的 SIGSEGV

c - 打印出指针指向的值(C编程)

c - SIGSEGV 段错误,不同的消息

c - 预处理器宏字符串化

c - 为什么在visual studio 2012中Output是这样的

c++ - 链接模板目标文件 - clang 和 gcc 的不同行为

linux - gcc 错误消息瘫痪(代码页或编码问题)