Search code examples
cfile-iosegmentation-faultdynamic-arrays

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


I have three files(written using the python .tofile function) with 1024x512 arrays in them with (double)values. I wrote This C program to open the files and load the values to memory array so that I can access them index wise. If i try to open just one file and dont try to read the other one, it runs fine and gives me the required values.For example THIS CODE WORKS LIKE A CHARM

#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);

}

But as soon as i try to read two or more files as shown below

#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);

}

I get a segmentation fault(core dumped) error. For Clarification: The file exists in the same directory as the code


Solution

  • Turns out, the stack size was the problem, setting ulimit -s 102400 (100MB) just to be safe, solved the problem!!