From 2c3376202d18ca9e1dffe664e9fd2e37b8f38aca Mon Sep 17 00:00:00 2001 From: Daniel Eisele Date: Thu, 24 Aug 2017 00:03:06 +0200 Subject: [PATCH] Removed the malloc calls to prevent memory leak at the end of the program --- hangman.c | 61 ++++++++++++++++++++++--------------------------------- hangman.h | 2 +- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/hangman.c b/hangman.c index 6863801..6084f7d 100644 --- a/hangman.c +++ b/hangman.c @@ -27,23 +27,18 @@ int main(int argc, char **argv) { }; int c, startscr = 1; /* Show startscreen by default */ char filename[255]; + game_state gs = {0}; + hitfeed hf = {{{0}}}; // 3 braces are needed to not get a warning + filename[0] = '\0'; /* Initialization */ initscr(); atexit(quitProgram); - game_state *gs; - hitfeed *hf; - gs = malloc(sizeof(game_state)); - hf = malloc(sizeof(hitfeed)); //zeroes the whole struct - initCoordinates(gs); - gs->allowedMoves = DEFAULTTRIES; - gs->moves = 0; - gs->guesses = 0; - gs->trollEnabled = 0; - hf->besthit = 0; - hf->impstreakcounter = 0; + initCoordinates(&gs); + gs.allowedMoves = DEFAULTTRIES; + keypad(stdscr, FALSE); nonl(); //No new line. Prevents the new line when hitting enter @@ -51,12 +46,12 @@ int main(int argc, char **argv) { while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) { switch (c) { case 'w': - sprintf(gs->guessWord, "%s", optarg); + sprintf(gs.guessWord, "%s", optarg); startscr = 0; break; case 'h': - showHelp(gs); - exit(0); + showHelp(&gs); + return 0; break; case 'f': /* Set filename */ @@ -68,34 +63,32 @@ int main(int argc, char **argv) { break; case 't': /* troll option */ - gs->trollEnabled = 1; + gs.trollEnabled = 1; break; default: - mvprintw(gs->centery, gs->centerx - 9, "Invalid arguments."); + mvprintw(gs.centery, gs.centerx - 9, "Invalid arguments."); getch(); - showHelp(gs); - exit(0); + showHelp(&gs); + return 0; break; } } InitializePRNG(time(NULL)); /* Initialize random number generator */ if (startscr) { - showStartScreen(gs); + showStartScreen(&gs); } /* start game */ - initGuessWord(gs, filename); - initAlphabet(gs); - drawAlphabet(gs, 0); - drawFigure(gs, 0); - startGame(gs, hf); + initGuessWord(&gs, filename); + initAlphabet(&gs); + drawAlphabet(&gs, 0); + drawFigure(&gs, 0); + startGame(&gs, &hf); nl(); //enable newline again while (getch() != 10); clear(); - free(gs); - free(hf); return 0; } @@ -168,19 +161,13 @@ void initGuessWord(game_state *gs, char *filename) { } void initAlphabet(game_state *gs) { - int hasNumber = 0; - int i; - for (i = 0; i < MAXWORDLENGTH; i++) { - if (isdigit(gs->guessWord[i]) > 0) { - hasNumber = 1; - break; - } - } - gs->alphabet = malloc(sizeof(char) * ((hasNumber ? ALPHABET : ALPHABET_NUM) + 1)); strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz"); - if (hasNumber) { - strcat(gs->alphabet, "1234567890"); + for (unsigned int i = 0; i < strlen(gs->guessWord); i++) { + if (isdigit(gs->guessWord[i]) > 0) { + strcat(gs->alphabet, "1234567890"); + break; + } } } diff --git a/hangman.h b/hangman.h index c26cc66..d444826 100644 --- a/hangman.h +++ b/hangman.h @@ -26,7 +26,7 @@ typedef struct { char currentWord[MAXWORDLENGTH]; char wrongCharacters[DEFAULTTRIES]; int trollEnabled; - char *alphabet; + char alphabet[ALPHABET_NUM + 1]; time_t startTime; time_t endTime; int wordRows;