尝试同时打开多个文件以从中读取时出现分段错误(核心已转储)

Segmentation fault(Core Dumped) while trying to open multiple files together to read from them

我有三个文件(使用 python .tofile 函数编写),其中包含 1024x512 数组和(双)值。我编写了这个 C 程序来打开文件并将值加载到内存数组,以便我可以通过索引访问它们。 如果我尝试只打开一个文件而不尝试读取另一个文件,它运行良好并为我提供了所需的 values.For 示例 此代码很有魅力

#include <stdio.h>
#include <stdlib.h>
#define xres 1024
#define yres 512
#define xgrid_spacing 0.025                                   //grid spacing along x
#define ygrid_spacing 0.025                                   //grid spacing along y
#define zgrid_spacing 1.0                                     //grid spacing along z

#define xbeg -12.8                                            //X coordinate limits
#define xend 12.8
#define ybeg -6.4                                             //Y coordinate limits
#define yend 6.4
#define zbeg 0.0                                              //Z coordinate limits
#define zend 1.0
void main(){

    const char file0[] = "bx1_1024X512.bin";
    const char file1[] = "bx2_1024X512.bin";
    const char file2[] = "v_crs_b_1024X512.bin";
    FILE *inp0;
    FILE *inp1;
    FILE *inp2;
double xx, yy, zz, xgrid_len, ygrid_len, zgrid_len;
    float xfloat, yfloat, zfloat;
    int xindex, yindex, zindex;

    //index counters
    int iind, jind, ind, jnd;
    //open the file

    double bx_elem, by_elem, bz_elem, v_crs_b_elem;
    inp0 = fopen(file0,"rb");
    inp1 = fopen(file1,"rb");
    inp2 = fopen(file2,"rb");
    //array to store the values
    double bxarray[xres][yres];
    double byarray[xres][yres];
    double minusv_crs_b_array[xres][yres];
    //iterate through the elements and save them to a memory array in C 

    for (iind=0;iind<xres;iind++)
    {
        for (jind=0;jind<yres;jind++)
        {
            fread(&bx_elem, sizeof(double), 1, inp0);
            bxarray[iind][jind]= bx_elem;

        }

    }

    //print the value to check
    printf("%lf\n", bxarray[0][0]);
    //close the file
    fclose(inp0);

}

但是一旦我尝试读取两个或更多文件,如下所示

#include <stdio.h>
#include <stdlib.h>
#define xres 1024
#define yres 512
#define xgrid_spacing 0.025                                   //grid spacing along x
#define ygrid_spacing 0.025                                   //grid spacing along y
#define zgrid_spacing 1.0                                     //grid spacing along z

#define xbeg -12.8                                            //X coordinate limits
#define xend 12.8
#define ybeg -6.4                                             //Y coordinate limits
#define yend 6.4
#define zbeg 0.0                                              //Z coordinate limits
#define zend 1.0
void main(){

    const char file0[] = "bx1_1024X512.bin";
    const char file1[] = "bx2_1024X512.bin";
    const char file2[] = "v_crs_b_1024X512.bin";
    FILE *inp0;
    FILE *inp1;
    FILE *inp2;
double xx, yy, zz, xgrid_len, ygrid_len, zgrid_len;
    float xfloat, yfloat, zfloat;
    int xindex, yindex, zindex;

    //index counters
    int iind, jind, ind, jnd;
    //open the file

    double bx_elem, by_elem, bz_elem, v_crs_b_elem;
    inp0 = fopen(file0,"rb");
    inp1 = fopen(file1,"rb");
    inp2 = fopen(file2,"rb");
    //array to store the values
    double bxarray[xres][yres];
    double byarray[xres][yres];
    double minusv_crs_b_array[xres][yres];
    //iterate through the elements and save them to a memory array in C 

    for (iind=0;iind<xres;iind++)
    {
        for (jind=0;jind<yres;jind++)
        {
            fread(&bx_elem, sizeof(double), 1, inp0);
            bxarray[iind][jind]= bx_elem;
            fread(&by_elem, sizeof(double), 1, inp1);
            byarray[iind][jind]= by_elem;

        }

    }

    //print the value to check
    printf("%lf\n", bxarray[10][30]);
    //close the file
    fclose(inp0);
    fclose(inp1);

}

我收到分段错误(核心转储)错误。 澄清一下:该文件与代码

存在于同一目录中

事实证明,堆栈大小是问题所在,设置 ulimit -s 102400 (100MB) 只是为了安全,解决了问题!!