Removed the malloc calls to prevent memory leak at the end of the program

This commit is contained in:
2017-08-24 00:03:06 +02:00
parent 76c6830b89
commit 2c3376202d
2 changed files with 25 additions and 38 deletions

View File

@@ -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) {
for (unsigned int i = 0; i < strlen(gs->guessWord); i++) {
if (isdigit(gs->guessWord[i]) > 0) {
strcat(gs->alphabet, "1234567890");
break;
}
}
}

View File

@@ -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;