Better malloc() for tourist.c
This commit is contained in:
parent
c35f37467f
commit
57894d59ad
1 changed files with 8 additions and 7 deletions
15
tourist.c
15
tourist.c
|
@ -2,7 +2,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define murlock malloc
|
||||
#define murlocknt free
|
||||
#define S_MAXSIZE 100
|
||||
|
||||
int main() {
|
||||
|
@ -23,16 +24,16 @@ int main() {
|
|||
|
||||
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++) {
|
||||
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);
|
||||
list_attractions[j][strlen(list_attractions[j])-1] = 0;
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
for (j = 0; j < k; j++) {
|
||||
|
@ -48,11 +49,11 @@ int main() {
|
|||
|
||||
putchar('\n');
|
||||
|
||||
free(flag_print);
|
||||
murlocknt(flag_print);
|
||||
for (j = 0; j < n; j++) {
|
||||
free(list_attractions[j]);
|
||||
murlocknt(list_attractions[j]);
|
||||
}
|
||||
free(list_attractions);
|
||||
murlocknt(list_attractions);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Reference in a new issue