Welcome to the Builder Academy

Question Confused over piece of code in parse_room()

More
26 Jan 2020 22:12 #8516 by thomas

If I understand correctly, because the original *end and mallocated *end have the same address as new_descr->description, plus *end being defined within this scope, means that *end disappears without having to free and null it.

Yes, that is exactly right - we've assigned the pointer new_descr->description to point to the same memory we were pointing to with the end pointer. We no longer need that, then.

Note that I've been quite a bit more descriptive in the new version of the code, in the pull request linked above.

Please Log in or Create an account to join the conversation.

More
29 Jan 2020 14:31 #8522 by krell
I declared the new function as static and set it up that way. It seems to be working fine after running it like that for a while. Is there any reason that this function should not be static? Are there plans to use this function outside of db.c?
Code:
static void ensure_newline_terminated(struct extra_descr_data *); . . . /* Fix for crashes in the editor when formatting. E-descs are assumed to * end with a \r\n. -Welcor */ static void ensure_newline_terminated(struct extra_descr_data* new_descr) { char *with_term, *end; if (new_descr->description == NULL) { return; } end = strchr(new_descr->description, '\0'); if (end > new_descr->description && *(end - 1) != '\n') { CREATE(with_term, char, strlen(new_descr->description) + 3); sprintf(with_term, "%s\r\n", new_descr->description); /* snprintf ok : size checked above*/ free(new_descr->description); new_descr->description = with_term; } }

Please Log in or Create an account to join the conversation.

More
31 Jan 2020 22:17 #8530 by thomas
Indeed, it can be static. It is - as far as I know - the only place it makes sense to use it.

Please Log in or Create an account to join the conversation.

Time to create page: 0.332 seconds