Aaron Troeger
Aaron Troeger

Reputation: 165

Segmentation fault (core dumped) c

My code is posted below, i'm pretty sure that the segmentation fault is happening in the while loop at the bottom of main().

My program has to read a file, the data is in a grid formation, the fscanf gets teh dimensions of the grid, then creates a 2D array with those dimensions, then i use a while loop to go through line by line and then a for loop to go through character by character in that line to store it in the inner part of the array, then i++ goes to store it in the next level of the 2D array.

can anyone tell me why its doing it?

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    char direction;
    int steps;
    int cur[2];
    char** gridArray;
    int* move(int cur[2]);
    int* getCommand(int next[2], char** gridArray);
    char** make2DFileArray(int sizeX, int sizeY);
    int* makeArray(int size);

    int main(int argc, char *argv[] )
    {
            FILE *fp;
            int rows;
            int columns;
            int i=0,j;
            char line[1000];

            if ((argc < 2)^(argc > 3)) {
                    fprintf(stderr, "Usage: thebox mapfile [maxsteps]\n");
                    exit(1);
            }
            if( argc < 3 ) { steps = 10; }
            else { steps = argv[2]; }

            fp = fopen(argv[1], "r");
            if( fp == NULL) {
                    fprintf(stderr, "Missing map file.\n");
                    exit(1);
            }

            fscanf(fp, "%d %d", &rows, &columns);
            gridArray = make2DFileArray(rows+1, columns);

            while ( fgets(line, 1000, fp) != NULL) {
                for(j=0; j < columns - 1; j++) {
                        gridArray[i][j] = line [j];
                }
                i++;
                printf("%s", line);
        }
        fclose(fp);
        printf("(sidepos)>\n");
        return 0;
}

and my helper method, is this

char** make2DFileArray(int sizeX, int sizeY)
{
        int i;
        char** array;
        array = (char**) malloc(sizeX*sizeof(char*));

        for (i = 0; i < sizeX; i++) {
                array[i] = (char*) malloc(sizeY*sizeof(char));
                return array;
        }
        return array;
}

int* makeArray(int size)
{
        int* array;
        array = (int*) malloc(size*sizeof(int));
        return array;
}

Upvotes: 0

Views: 9465

Answers (3)

Yue Yu
Yue Yu

Reputation: 21

steps = argv[2]; 

The variable 'steps' type is int, and variable 'argv[2]' type is char *, can't be directly assigned.

You can do it by

steps = atoi(argv[2]);

Upvotes: 1

Jacob Pollack
Jacob Pollack

Reputation: 3751

This:

for (i = 0; i < sizeX; i++) {
  array[i] = (char*) malloc(sizeY*sizeof(char));
  return array;
}

return array;

... should be:

for (i = 0; i < sizeX; i++) {
  array[i] = (char*) malloc(sizeY*sizeof(char));
}

return array;

... in your function make2DFileArray.

Remark: The element is allocated memory however the rest of the elements are not because of your return inside the for-loop. Each element in the array from [1 ... length - 1] are random memory addresses (because when you malloc you did not populate them as proper pointers) and hence dereferencing any element other than the one at position 0 will result in undefined behaviour.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

The error is in the make2DFileArray function. You just return after the first allocation in the loop, meaning only gridArray[0] will be allocated.

Since none of the other entries will be allocated, accessing e.g. gridArray[1] is undefined behavior and the cause of your crash.

Upvotes: 4

Related Questions