简单程序上非常奇怪的段错误

时间:2015-04-20 15:03:11

标签: c segmentation-fault

除了非常奇怪之外,我不知道如何描述这个错误。我完全不知道出了什么问题。我们的教授要求我们使用pre-ANSI C语法。当我尝试打印y [0]时,程序seg出错。所以该程序的输出是:

输出

1.0000000000000000
before
-0.500000
Segmentation fault (core dumped)

程序

int main (int argc, char* argv[]) {                                             

    int n = 2;                                                                                                                                                                              
    static double a[2];                                                         
    static double temp[2][2];                                                   
    static double t = 0;                                                        
    static double y0[2];                                                        
    static double y[2];

    temp[0][0] = 1;                                                             
    temp[0][1] = 2;                                                             
    temp[0][2] = 2;                                                             
    temp[1][0] = 1;                                                             

    y[0] = 1;                                                                   
    y[1] = 2;
    y0[0] = -.5;
    y0[1] = .5;


    printf("%.16f\n",y[0]);                                                                                                                    
    stiff_solve(temp, n, y0, t, 0, y);                                          
} 

 void stiff_solve(a, n, y0, t, m, y)                                             
 int n;                                                                          
 double a[n][n];                                                                 
 double y0[n];                                                                   
 double t;                                                                       
 double m;                                                                       
 double y[n];                                                                    
 {
     printf("before\n");                                                         
     printf("%f\n",y0[0]);                                                       
     printf("%f\n",y[0]);                                                        
     printf("after\n");        
 }

Valgrind的

==5922== Memcheck, a memory error detector
==5922== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5922== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==5922== Command: ./numcomp
==5922== 
1.0000000000000000
before
-0.500000
==5922== Invalid read of size 8
==5922==    at 0x400934: stiff_solve (stiff.c:83)
==5922==    by 0x40084E: main (stiff.c:48)
==5922==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5922== 
==5922== 
==5922== Process terminating with default action of signal 11 (SIGSEGV)
==5922==  Access not within mapped region at address 0x0
==5922==    at 0x400934: stiff_solve (stiff.c:83)
==5922==    by 0x40084E: main (stiff.c:48)
==5922==  If you believe this happened as a result of a stack
==5922==  overflow in your program's main thread (unlikely but
==5922==  possible), you can try to increase the size of the
==5922==  main thread stack using the --main-stacksize= flag.
==5922==  The main thread stack size used in this run was 8388608.
==5922== 
==5922== HEAP SUMMARY:
==5922==     in use at exit: 0 bytes in 0 blocks
==5922==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==5922== 
==5922== All heap blocks were freed -- no leaks are possible
==5922== 
==5922== For counts of detected and suppressed errors, rerun with: -v
==5922== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Segmentation fault (core dumped)

1 个答案:

答案 0 :(得分:3)

temp[0][2] = 2;

数组越界访问将导致未定义的行为