Seg fault: Likely pointer or Malloc issue.

The code reads from a file and outputs a 'maze' diagram. The seg fault occurs immediately (even a printf right after main doesn't come through).
I receive warnings at:
81)comparison between pointer and integer
83)argument 2 is and interger
All in bold.

After running GDB, backtrace returns:
#0 0x00007ffd1acb0a6b in ?? () from /lib/libc.so.6
#1 0x00007ffd1acada10 in atoi () from /lib/libc.so.6
#2 0x0000000000400941 in main ()
GDB won't let me step through cuz it faults out right at the start.

I would appreciated any suggestions on pointers or how to correct the seg fault.
However, I do have to use strtok(I know it sucks) and I must stay within C.

EDIT: It doesn't seem to like they way I set array = to stuff. If I comment it out, there is no segfault. Suggestions?

Thanks in advance for your masterful skillz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define INPUT_SIZE 201

int main(int argc, char **argv)
{
    printf("I'm working!");
    int j;                                      //column
    int i = 0;                                  //Row
    char *token;                                //Tokens
    char *row;                                  //Row marks
    int  rows;                                  // Number of Rows

    // Open and read from file
    FILE *fin;
    fin = fopen(argv[1], "r");

    //Allocate memory for input
    char *input = (char *)malloc(sizeof(char) *INPUT_SIZE);

    //Read in number of Rows
    fscanf(fin, "%d", &rows);

    // Allocate memory for output
    char **array = (char **)malloc(sizeof(char *) *rows);

    //Get the line
    while(fgets(input, INPUT_SIZE, fin))
    {
        char *first_field = strtok(input, " ");

        //If Number, get boarder info
        if(atoi(first_field) != 0)
        {
            *array = (char *)malloc(sizeof(char) *atoi(first_field));

            //Initialize to space
for(j = 0; j < atoi(first_field); j++)
                array[i][j] = 0;

            while((token = strtok(NULL, " ")) !=NULL);
            {
                char *position = strchr(token, '-');

                //Single value?
                if(position == NULL)
                {
                    array[i][atoi(token)] = '#';
                }
                else
                {
                    //It's a range
                    (*position) = '\0';
                    int start = atoi(token);
                    int end = atoi(position +1);

                    while(start <= end)
                    {
                        array[i][start] = '#';
                        start++;
                    }
                }
            }
        }
        //It's a symbol
        else
        {
            token = strtok(NULL, " ");
            row = strtok(NULL, " ");
            array[atoi(row)][atoi(token)] = *first_field;
        }

        i++;
    }

    //Print maze
    for(i =0; i <= rows; i++)
    {
        for(j =0; array[i][j] != NULL; j++)
        {
            printf("%s", array[i][j]);
        }
        printf("\n");
    }
    return 0;
}



The input is just a text file.
Line 1 is the number of rows in the maze
Rows are input next, then players, monsters, and treasure
The first entry in a row denotes the width of the row in characters and the remaining entries use the representation x or x-y to indicate either a single # at x or a line of #'s from x to y.
A player, monster, or piece of treasure will be input on individual lines with a symbol, and an (x,y) location.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
9
29 0-28
29 0 28
34 0 4-8 12-16 28-33
34 0 4-8 13-14 33
34 0 5-7 13-14 20-26 33
34 0 6-7 12-16 20-26 33
34 0 7 33
34 0 33
34 0-33
@ 1 4
$ 2 21
! 4 12
O 7 11
$ 7 19
D 7 25
Last edited on
I'm not sure where you're getting the segfault, so sorry that I can't help you with that, but I think
fgets(input, INPUT_SIZE, fin);
will produce a buffer overrun if the user enters INPUT_SIZE. You want fgets to get INPUT_SIZE - 1 instead, so that the string can be NULL'd.
The problems are:

1
2
3
int rows; // unitialized and you are using it next in
    //Read in number of Rows
    fscanf(fin, "%d", &rows);


Next problem could be that, if user does not enter a filename then your program will still continue running, and will try to open a blank file (since argv[1] is nothing). To prevent this you can use,

1
2
3
4
5
6

if (argc < 2) { 

    printf("error: usage <executable> <filename>\n");
    exit(1); 
}


Next problem, you are using atoi for allocating memory to array in

*array = (char *)malloc(sizeof(char) *atoi(first_field));

Now on failure, atoi returns 0 which is a valid int, no in your case just imagine if atoi fails, how much memory will
be allocated?

Other than that, it would help if you could post your text file which you want to read.

Hope this helps !
@ kevinchkin: Error checking isn't the issue. I just removed it to get rid of some of the clutter. I'll check out the rest of your suggestions. Thanks for the reply.

EDIT: Well, for *array = (char *)malloc..... I checked to be sure first_fields is a number right above it. If it's not a number, then it's a special character and gets put directly into the coordinates proceeding it. So, it shouldn't ever be failing at that point. I replaced it with a constant just to be sure. No change.
Last edited on
Topic archived. No new replies allowed.