How to Fix Segmentation fault (core dumped) Error : <p>I am trying to read some values from a txt file and load it into mem, this is actually a dataset for neural network training, I am getting a segmentation fault when the following function is called, how can i fix this error, my main issue is this error does not show it is thrown, how can i find the line and resolve it. 
This function takes a file pointer and allocates mem to a strcut named dataset, which consist of data_members as shown below and split into training and testing data</p>

<pre><code>typedef struct data_member {
    double*         inputs;         /* The input data */
    double*         targets;        /* The target outputs */
} data_member;

typedef struct dataset{
    data_member*    members;        /* The members of the dataset */
    int             num_members;    /* The number of members in the set */
    int             num_inputs;     /* The number of inputs in the set */
    int             num_outputs;    /* The number of outputs in the set */
} dataset;

</code></pre>

<p>the function</p>

<pre><code>
dataset *load_dataset(FILE *datafile, double ratio, dataset *testset)
{
    dataset *trainset;
    int num_inputs, num_outputs, num_members;
    int i, j;
    double temp=0.0;
    char trainset_check = 0x00;
    char testset_check = 0x00;

    /* Load first line which contain settings */
    fscanf(datafile, "%d, %d, %d\n", &amp;num_members, &amp;num_inputs, &amp;num_outputs);

    /* Set ratio of split*/
    int trainset_size = (int) num_members * (1- ratio);
    int testset_size = (int)  num_members * (ratio);

    printf("size of train:%d, test: %d\n", trainset_size, testset_size);

    /* Setup the dataset */
    /* Allocate memory for the trainset */
    if ((trainset = (dataset *)malloc(sizeof(dataset))) == NULL)
    {
        perror("Couldn't allocate the trainset\n");
        return (NULL);
    }

    /* Allocate memory for the testset */
    if ((testset = (dataset *)malloc(sizeof(dataset))) == NULL)
    {
        perror("Couldn't allocate the testset\n");
        return (NULL);
    }

    /* Set the variables trainset*/
    trainset-&gt;num_members = trainset_size;
    trainset-&gt;num_inputs = num_inputs;
    trainset-&gt;num_outputs = num_outputs;

    /* Set the variables testset*/
    testset-&gt;num_members = testset_size;
    testset-&gt;num_inputs = num_inputs;
    testset-&gt;num_outputs = num_outputs;

    /* Allocate memory for the arrays in the trainset */
    if ((trainset-&gt;members = (data_member *)malloc(trainset_size * sizeof(data_member))) != NULL)
        trainset_check |= 0x01;

    /* Allocate memory for the arrays in the testset */
    if ((trainset-&gt;members = (data_member *)malloc(testset_size * sizeof(data_member))) != NULL)
        testset_check |= 0x01;

    if (trainset_check &lt; 1)
    {
        printf("1.Couldn't allocate trainset\n");
        if (trainset_check &amp; 0x01)
            free(trainset-&gt;members);
        free(trainset);
        return (NULL);
    }

    if (testset_check &lt; 1)
    {
        printf("1.Couldn't allocate testset\n");
        if (testset_check &amp; 0x01)
            free(testset-&gt;members);
        free(testset);
        return (NULL);
    }






    /* Get the data for trainset */
    /* For each Member */
    for (i = 0; i &lt; trainset_size; i++)
    {
        printf("itr:%d\n", i);
        /* Allocate the memory for the member */
        trainset_check = 0x00;
        if (((trainset-&gt;members + i)-&gt;inputs = (double *)malloc(num_inputs * sizeof(double))) != NULL)
            trainset_check |= 0x01;
        if (((trainset-&gt;members + i)-&gt;targets = (double *)malloc(num_outputs * sizeof(double))) != NULL)
            trainset_check |= 0x03;

        if (trainset_check &lt; 3)
        {
            printf("2.Couldn't allocate trainset\n");
            /* Deallocate the previous loops */
            for (j = 0; j &lt; i; j++)
            {
                free((trainset-&gt;members + j)-&gt;inputs);
                free((trainset-&gt;members + j)-&gt;targets);
            }

            if (trainset_check &amp; 0x01)
                free((trainset-&gt;members + i)-&gt;inputs);
            if (trainset_check &amp; 0x02)
                free((trainset-&gt;members + i)-&gt;targets);

            free(trainset-&gt;members);
            free(trainset);
            return (NULL);
        }

        /* Read the inputs */
        for (j = 0; j &lt; num_inputs; j++)
        {
            fscanf(datafile, "%lf, ", &amp;temp);
            printf("%lf, ", temp);
            (trainset-&gt;members + i)-&gt;inputs[j] = temp;
        }

        /* Read the outputs */
        for (j = 0; j &lt; num_outputs - 1; j++)
        {
            fscanf(datafile, "%lf, ", &amp;temp);
            printf("%lf, ", temp);
            (trainset-&gt;members + i)-&gt;targets[j] = temp;
        }
        fscanf(datafile, "%lf\n", &amp;temp);
        printf("%lf\n", temp);
        printf("****************\n");
        (trainset-&gt;members + i)-&gt;targets[j] = temp;
        printf("########################%d\n",i);
    }

    printf("done loading trainset");


    /* Get the data for testset */
    /* For each Member */
    for (i = 0; i &lt; testset_size -1; i++)
    {
        /* Allocate the memory for the member */
        testset_check = 0x00;
        if (((testset-&gt;members + i)-&gt;inputs = (double *)malloc(num_inputs * sizeof(double))) != NULL)
            testset_check |= 0x01;
        if (((testset-&gt;members + i)-&gt;targets = (double *)malloc(num_outputs * sizeof(double))) != NULL)
            testset_check |= 0x03;

        if (testset_check &lt; 3)
        {
            printf("2.Couldn't allocate testset\n");
            /* Deallocate the previous loops */
            for (j = 0; j &lt; i; j++)
            {
                free((testset-&gt;members + j)-&gt;inputs);
                free((testset-&gt;members + j)-&gt;targets);
            }

            if (testset_check &amp; 0x01)
                free((testset-&gt;members + i)-&gt;inputs);
            if (testset_check &amp; 0x02)
                free((testset-&gt;members + i)-&gt;targets);

            free(testset-&gt;members);
            free(testset);
            return (NULL);
        }

        /* Read the inputs */
        for (j = 0; j &lt; num_inputs; j++)
        {
            fscanf(datafile, "%lf, ", &amp;temp);
            printf("%lf, ", temp);
            (testset-&gt;members + i)-&gt;inputs[j] = temp;
        }

        /* Read the outputs */
        for (j = 0; j &lt; num_outputs - 1; j++)
        {
            fscanf(datafile, "%lf, ", &amp;temp);
            printf("%lf, ", temp);
            (testset-&gt;members + i)-&gt;targets[j] = temp;
        }
        fscanf(datafile, "%lf\n", &amp;temp);
        printf("%lf\n", temp);
        (testset-&gt;members + i)-&gt;targets[j] = temp;
    }

    /* Make sure the file is closed */
    fclose(datafile);

    /* Finally, return the pointer to the dataset */
    return (trainset);
}


</code></pre>
