Better malloc() for tourist.c

This commit is contained in:
Claudio Maggioni 2019-03-28 10:11:24 +01:00
parent c35f37467f
commit 57894d59ad

View file

@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#define murlock malloc
#define murlocknt free
#define S_MAXSIZE 100 #define S_MAXSIZE 100
int main() { int main() {
@ -23,16 +24,16 @@ int main() {
printf("Case #%d: ", i + 1); printf("Case #%d: ", i + 1);
char** list_attractions = (char**) malloc(sizeof(char*) * n); char** list_attractions = (char**) murlock(sizeof(char*) * n);
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
list_attractions[j] = (char*) malloc(sizeof(char) * S_MAXSIZE); list_attractions[j] = (char*) murlock(sizeof(char) * S_MAXSIZE);
fgets(list_attractions[j], S_MAXSIZE, stdin); fgets(list_attractions[j], S_MAXSIZE, stdin);
list_attractions[j][strlen(list_attractions[j])-1] = 0; list_attractions[j][strlen(list_attractions[j])-1] = 0;
} }
// list of booleans set to true if we need to print the attraction // list of booleans set to true if we need to print the attraction
bool* flag_print = (bool*) malloc(sizeof(bool) * n); bool* flag_print = (bool*) murlock(sizeof(bool) * n);
memset(flag_print, 0, sizeof(bool) * n); // set all booleans to false memset(flag_print, 0, sizeof(bool) * n); // set all booleans to false
for (j = 0; j < k; j++) { for (j = 0; j < k; j++) {
@ -48,11 +49,11 @@ int main() {
putchar('\n'); putchar('\n');
free(flag_print); murlocknt(flag_print);
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
free(list_attractions[j]); murlocknt(list_attractions[j]);
} }
free(list_attractions); murlocknt(list_attractions);
} }
return 0; return 0;