Search code examples
cfilematrixsegmentation-faultstdin

Segmentation Fault (core dumped) when allocating memory for 2D array


I'm trying to read specific chars inside a file redirected from stdin into a 2D array, I'm not sure if I'm allocating the memory for the 2D array properly. The first line inside the file is the dimensions of the matrix I'm trying to copy.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "QueueImplementation.c"

int tester(char *s, int cnt)
{
        int num1, num2;
        if(cnt == 0)
        {
                sscanf(s, "%d %d", &num1, &num2);
                if(num1 < 2 || num1 > 20 || num2 < 2 || num2> 20)
                {
                        printf("Incorrect Matrix Dimensions!");
                        printf("\n");
                }
                return num1;
        }
}

void allocateMem(char ***cell, int n, int m)
{
        *cell=(char**)malloc(n*sizeof(int*));
        for(int i=0;i<n;i++)
                *cell[i]=(char*)malloc(m*sizeof(int));
}
int main(){
        char buffer[200];
        int j,max_row,max_col;
        int count = 0;
        int i = 0;
        while (fgets(buffer, sizeof buffer, stdin))
        {
                if(count == 0)
                        max_col = tester(buffer, count);
        count++;
        }
        max_row = count - 1;
        char** cell;
        allocateMem(&cell, max_row, max_col);
        while (fgets(buffer, sizeof buffer, stdin))
        {
                for(j = 0;j<max_col;j++)
                {
                        if(buffer[j] != '\n' && buffer[j] != ' ' && buffer[j] < '0')
                                cell[i-1][j] = (char) buffer[j];
                }
                i++;
        }
        for (i = 0;i<max_row;i++)
        {
                for (j = 0;j<max_col;j++)
                {
                        printf("%c", cell[i][j]);
                }
        }
}

Test file that I redirect consists of

12 10
oooooooooooo
ooooooooooo.
oooooooo....
se.......ooo
oooooooo....

Mainly consists of "o" and "." except for a single "s" and "e". The 12 and 10 are the dimensions of the matrix that I am trying to copy, so the expected output should be the matrix consisting the o's and .'s along with a single "s" and "e".


Solution

  • fix like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void tester(char *s, int *cols, int *rows){
        int cnt;
    
        cnt = sscanf(s, "%d %d", cols, rows);
        if(cnt != 2 || *cols < 2 || *cols > 20 || *rows < 2 || *rows > 20){
            printf("Incorrect Matrix Dimensions!\n");
            exit(EXIT_FAILURE);//The program can not be continued.
        }
    }
    
    void allocateMem(char ***cell, int n, int m){
        *cell = malloc( n * sizeof(char*));
        for(int i = 0; i < n; i++)
            (*cell)[i] = malloc(m * sizeof(char));
    }
    
    int main(void){
        char buffer[200] = "";
        char **cell;
        int max_row, max_col;
        int i, j;
    
        fgets(buffer, sizeof buffer, stdin);//read first line
        tester(buffer, &max_col, &max_row);//If fgets fails, this also fails
    
        allocateMem(&cell, max_row, max_col);
    
        for(i = 0; i < max_row; ++i){
            for(j = 0; j < max_col; j++){
                int ch = fgetc(stdin);
                if(!isspace(ch))
                    cell[i][j] = ch;
                else
                    --j;//cancel this turn
            }
        }
        for (i = 0; i < max_row; i++){
            for (j = 0; j < max_col; j++){
                printf("%c", cell[i][j]);
            }
            puts("");//put newline
        }
        //deallocate cell
    }