From: Christian Heller Date: Sat, 25 Jan 2014 19:13:19 +0000 (+0100) Subject: Removed unnecessary NULLings of pointers, explained kept ones. X-Git-Tag: tce~865 X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7B%20deck_id%20%7D%7D/template?a=commitdiff_plain;h=316a56901b2249264b72992fd5aa63ce16fd3304;p=plomrogue Removed unnecessary NULLings of pointers, explained kept ones. --- diff --git a/src/client/command_db.c b/src/client/command_db.c index 7d35582..159eb58 100644 --- a/src/client/command_db.c +++ b/src/client/command_db.c @@ -73,10 +73,10 @@ extern void init_command_db() copy_tokenized_string(line, &world.cmd_db.cmds[i].dsc_short, delim); copy_tokenized_string(NULL, &world.cmd_db.cmds[i].server_msg, delim); if (!strcmp("0", world.cmd_db.cmds[i].server_msg)) - { - free(world.cmd_db.cmds[i].server_msg); - world.cmd_db.cmds[i].server_msg = NULL; - } + { /* .server_msg==0 detects */ + free(world.cmd_db.cmds[i].server_msg); /* non-server commands in */ + world.cmd_db.cmds[i].server_msg = NULL;/* control.h's try_key() / */ + } /* try_server_command(). */ char * arg_string = strtok(NULL, delim); world.cmd_db.cmds[i].arg = arg_string[0]; copy_tokenized_string(NULL, &world.cmd_db.cmds[i].dsc_long, "\n"); diff --git a/src/client/draw_wins.c b/src/client/draw_wins.c index 9fd78fc..54191f6 100644 --- a/src/client/draw_wins.c +++ b/src/client/draw_wins.c @@ -358,12 +358,12 @@ extern void draw_win_inventory(struct Win * win) win->center.y = world.player_inventory_select; char inventory_copy[strlen(world.player_inventory) + 1]; sprintf(inventory_copy, "%s", world.player_inventory); - char * foo = inventory_copy; + char * strtok_target = inventory_copy; uint8_t i = 0; while (1) { - char * object = strtok(foo, "\n"); - foo = NULL; + char * object = strtok(strtok_target, "\n"); + strtok_target = NULL; if (NULL == object) { return; diff --git a/src/client/io.c b/src/client/io.c index 095577d..4623b3d 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -5,6 +5,7 @@ #include /* open() */ #include /* PIPE_BUF */ #include /* halfdelay(), getch() */ +#include /* NULL */ #include /* uint8_t, uint16_t, uint32_t */ #include /* FILE, sprintf(), fseek() */ #include /* strcmp(), strlen(), memcpy() */ @@ -78,7 +79,7 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) char * f_name = "read_inventory()"; char * delimiter = "%\n"; free(world.player_inventory); - world.player_inventory = NULL; + world.player_inventory = NULL; /* Avoids illegal strlen() below. */ while (1) { try_fgets(read_buf, linemax + 1, file, f_name); diff --git a/src/client/main.c b/src/client/main.c index 3e3bbdd..2b6c18a 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -2,6 +2,7 @@ #include /* keypad() */ #include /* struct sigaction, sigaction() */ +#include /* NULL */ #include /* exit() */ #include /* memset() */ #include "../common/rexit.h" /* set_cleanup_func(), exit_trouble() */ diff --git a/src/client/misc.c b/src/client/misc.c index ca8e829..839ce36 100644 --- a/src/client/misc.c +++ b/src/client/misc.c @@ -98,11 +98,8 @@ extern void load_interface_conf() extern void unload_interface_conf() { free_keybindings(world.kb_global.kbs); - world.kb_global.kbs = NULL; free_keybindings(world.kb_wingeom.kbs); - world.kb_wingeom.kbs = NULL; free_keybindings(world.kb_winkeys.kbs); - world.kb_winkeys.kbs = NULL; while ('\0' != world.windb.active) { toggle_window(world.windb.active); diff --git a/src/client/windows.c b/src/client/windows.c index 572fccd..6bde9ef 100644 --- a/src/client/windows.c +++ b/src/client/windows.c @@ -483,8 +483,8 @@ static void draw_wins(struct Win * w) w->start.x + (x - offset_x), ch); } } - free(w->winmap); - w->winmap = NULL; + free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */ + w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/ memset(&w->winmap_size, 0, sizeof(struct yx_uint16)); if (offset_y > 0) { @@ -734,12 +734,10 @@ extern void free_windb() free(wc->title); free_keybindings(wc->kb.kbs); } - free(world.windb.ids); - world.windb.ids = NULL; + free(world.windb.ids); /* NULL this too since add_win_to_windb() checks */ + world.windb.ids = NULL; /* for it to detect its first post-DB-purge round.*/ free(world.windb.wins); - world.windb.wins = NULL; free(world.windb.order); - world.windb.order = NULL; } diff --git a/src/server/init.c b/src/server/init.c index 1644cee..97ed0fa 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -49,15 +49,12 @@ extern void obey_argv(int argc, char * argv[]) extern void remake_world(uint32_t seed) { free(world.log); - world.log = NULL; + world.log = NULL; /* map_object_action.c's update_log() checks for this. */ world.seed = seed; world.map_obj_count = 0; world.score = 0; free(world.map.cells); - if (world.map_objs) - { - free_map_objects(world.map_objs); - } + free_map_objects(world.map_objs); world.last_update_turn = 0; world.turn = 1; init_map(); diff --git a/src/server/io.c b/src/server/io.c index 800f539..817d542 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -66,8 +66,8 @@ static char * get_message_from_queue() world.queue_size = world.queue_size - cutout_len; if (0 == world.queue_size) { - free(world.queue); - world.queue = NULL; + free(world.queue); /* NULL so read_fifo_into_queue() may free() this */ + world.queue = NULL; /* every time, even when it's un-allocated first. */ } else { diff --git a/src/server/map_objects.c b/src/server/map_objects.c index abcb6ac..36fb9b7 100644 --- a/src/server/map_objects.c +++ b/src/server/map_objects.c @@ -134,7 +134,6 @@ extern void free_map_object_defs(struct MapObjDef * mod_start) free_map_object_defs(mod_start->next); free(mod_start->name); free(mod_start); - mod_start = NULL; } @@ -159,10 +158,10 @@ extern void free_map_objects(struct MapObj * mo_start) free_map_objects(mo_start->owns); free_map_objects(mo_start->next); free(mo_start); - if (mo_start == world.map_objs) - { - world.map_objs = NULL; - } + if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */ + { /* map object iteration loop does not */ + world.map_objs = NULL; /* iterate over freed memory when called */ + } /* the 1st time after world re-seeding. */ }