我正在运行一个代码来计算和打印卡方和文件的卡方密度文件的值;但是我的问题是我在屏幕输出结束时得到了一个分段错误错误,即使点数相对较少,例如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;
}
答案 0 :(得分:2)
在main
for
循环中,i
等于N
,因此阅读&amp;写入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{
...
内),您也必须返回一个值:
{{1}}