1

I am working on a programming assignment in C, which is about creating basic automation for cinema halls.

For holding data of halls, I define a structure like this:

typedef struct {
 char *hallName;
 char *movieName;
 seat** hallSeats;
 int studentCount;
 int fullFareCount;
 int totalSum;
 int width;
 int height;
}Hall;

So I am given a text file with commands and whenever I came up with a specific command, I should create a separate hall. For that reason, I created another function for that.

Hall makeHall(char **temp)       //TEMP HOLDING THE LINES FROM FILE
 {
    int width = strToInt(temp[3]);
    int height = strToInt(temp[4]);

    char currentRowLetter = 'A';

    int currentRow;
    int currentSeat;

    seat **hall = malloc(sizeof(seat*) * width );

    for (currentRow=0 ; currentRow < width ; currentRow++)
    {
         hall[currentRow] = malloc(sizeof(seat) * height );

        for(currentSeat=0; currentSeat < height ; currentSeat++)
        {
            hall[currentRow][currentSeat].rowLetter = currentRowLetter;
            hall[currentRow][currentSeat].seatNumber = currentSeat + 1;
            hall[currentRow][currentSeat].seatTaken = ' ';
        }
        ++currentRowLetter;
    }

    Hall newHall;
    newHall.hallName = temp[1];
    newHall.movieName = temp[2];
    newHall.hallSeats = hall;
    newHall.width = width;
    newHall.height = height;

    return newHall;
}

Since I will have multiple halls, I created a Hall array in order to access them later.

    Hall *allHalls = malloc(sizeof(Hall) * 10);    /*Hall placeholder*/

While I iterate over the lines, I check commands and create halls or sell tickets.

Hall *allHalls = malloc(sizeof(Hall) * 10);                       /*Hall placeholder*/
FILE *f;
f = fopen("input.txt", "rt");
char *line = malloc (sizeof(char) * 200);   /*LINE HOLDER*/
int currentLineNumber = 0;
char *tmp;
int hallNumber = 0;

while (1) {     /*PARSING FILE*/
    if (fgets(line,200, f) == NULL) break;      /*FILE END CHECKER*/
    currentLineNumber++;
    tmp = strtok(line," ");
    char **temp = malloc(sizeof(char*) * 6);
    int currentWordNumber = 0;

    while(tmp != NULL)     /*PARSING LINES*/
    {
        temp[currentWordNumber] = malloc(strlen(tmp) + 1);
        strcpy(temp[currentWordNumber],tmp);
        tmp = strtok (NULL, " ");
        currentWordNumber++;
    }
    if(!strcmp("CREATEHALL",temp[0]))  
    {
        allHalls[hallNumber] = makeHall(temp); /*<<<<<<<PROBLEM*/
        hallNumber++;   
        printf("%d\n",hallNumber);
    }

Now that's the part I am lost at. Whenever I tried to access the array, the program crashes.

I thought it was a memory problem, so increased memory allocated by malloc for allHalls to 40 (even though it should not be a problem, since file only gives 3 different halls) and program no longer crashes, but instead overwrites the previous hall in the array.

I tried multiple solutions but none of them came out any good, so closest I get is this.

I did use java a lot before, so I am still stuck to OOP and pretty new to C.

EDIT Seat is defined as

typedef struct {
char rowLetter;
int seatNumber;
char seatTaken;

}seat;

also example createhall command is

CREATEHALL Hall_A Avatar 24 20

while the numbers at the end being width and height for hall

EDIT : CODE

20
  • Could you show us how an example file looks like? And how is seat defined? Commented Oct 24, 2016 at 18:34
  • Please provide the definition of seat. Commented Oct 24, 2016 at 18:41
  • 3
    Please provide a minimal reproducible example. Can't chase after bugs in multiple non-buildable program fragments. Commented Oct 24, 2016 at 18:43
  • 1
    sorry for that. Still new here. trying to improve question now Commented Oct 24, 2016 at 18:46
  • 1
    @Elyasin, seen no error there. tmpis initialized from strtok Commented Oct 24, 2016 at 18:59

1 Answer 1

2

I got the bug:

At the bottom of the while(1) loop in main you do a free(allHalls); so now there are no more halls and you get a segfault...

It was in the code you didn't show us:

while (1) {
    ...
    if(!strcmp("CREATEHALL",temp[0]))  
    {
        allHalls[hallNumber] = makeHall(temp); /*<<<<<<<PROBLEM*/
        hallNumber++;   
        printf("%d\n",hallNumber);
    }
    ....
    free(temp);
    free(allHalls);      // <-- there's your bug
}
fclose(f);
free(line);
Sign up to request clarification or add additional context in comments.

1 Comment

As an afternote: You could have found this yourself if you had stepped the program through the debugger. Use your tools!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.