honorlessman honorlessman - 1 year ago 55
C Question

Creating a structure array in C

I am working on programming assignment on 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;

so I have given with a text file with commands and whenever I came up with specific command I should create a seperate 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 = ' ';

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*/
tmp = strtok(line," ");
char **temp = malloc(sizeof(char*) * 6);
int currentWordNumber = 0;

while(tmp != NULL) /*PARSING LINES*/
temp[currentWordNumber] = malloc(strlen(tmp) + 1);
tmp = strtok (NULL, " ");
allHalls[hallNumber] = makeHall(temp); /*<<<<<<<PROBLEM*/

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

I thought it is memory problem so increased memory allocated by malloc for allHalls to 40 (even thought 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.

Seat is defined as

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


also example createhall command is

CREATEHALL Hall_A Avatar 24 20

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


Answer Source

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) {
        allHalls[hallNumber] = makeHall(temp); /*<<<<<<<PROBLEM*/
    free(allHalls);      // <-- there's your bug
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download