2013-11-28 30 views
0

我正在運行一個代碼來計算和打印卡方密度文件的值以屏幕和文件;然而,我的問題是,即使在相對較低的點數(如100,500等)的情況下,我也會在屏幕輸出結束時得到分段錯誤錯誤。我寫了一個代碼,它利用更多的數字並且運行時沒有問題。如果您指出錯誤並指導我採取補救措施,我將不勝感激。當我嘗試使用malloc我得到一個不同的錯誤如下:用於低迭代次數的C中的分段錯誤

*** 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; 
} 
+2

'我<= N'應'I Kninnug

回答

2

mainfor循環,直到i等於N因此讀&書面x[N]y[N]它落在分配數組之外。 (對於聲明大小爲Nint 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{ 
    ...