Finish the Capstone: the remove Command
HardYou are given the capstone program complete except for one command, and it is the command that undoes an add. main already has a remove branch wired to inventory_remove(struct Inventory *inventory, const char *name), which returns 1 when it removed something and 0 when nothing carried that name; on 1 main prints removed followed by the name on standard output, and on 0 it reports the miss on standard error and prints nothing at all. Everything else is finished and correct: make_item and free_item, the doubling append in inventory_add, inventory_find, inventory_total, inventory_free, and the fgets and sscanf command loop. What you write is the body of inventory_remove, which the template stubs out so that the program still compiles warning-free and still runs with defined behaviour without it, reporting remove: not implemented on standard error and failing only the test cases that use remove. Three steps. First, call inventory_find(inventory, name), which hands back a pointer to the element or NULL when no item carries that name, and return 0 immediately on NULL, since main is the one that reports the miss. Second, free the name of the element you found with free_item, because the array element owns that string: the assignment inside inventory_add copied a pointer rather than the characters, and from that moment the element is the only owner, so nothing else in the program will ever free it and skipping this step leaks it on every single removal. fail_on_memory_leak is set on this exercise, so a run that prints every expected line and loses one name still fails. Third, close the gap the removal left, and the way to do it is to copy the LAST element over the one you have just emptied: *found = inventory->items[inventory->count - 1]; followed by inventory->count -= 1;. That one struct assignment moves all three members, the surviving name pointer among them, so the slot has an owner again and the element that used to sit at the end is no longer reachable at its old index, which is exactly why the decrement has to come after it. Do it unconditionally, including when the element you found is already the last one, since a struct assigned to itself is defined and costs nothing while an if written to avoid it is one more branch to get wrong. The order of those two steps is chapter 6's order in a new place: free the name first and copy over the slot second, because copying first would overwrite the only pointer to the string and leak it, and freeing after the copy would free the survivor's name instead and leave a freed pointer sitting in the array. Note what removal deliberately does not do. It does not shrink the array and it does not call realloc, so capacity stays wherever the doublings left it, the surviving items keep their addresses except for the one that moved, and inventory_free at the end still frees exactly one block plus one name for each of the count items still present. Nothing else in the file needs changing. No message wording is checked, because every diagnostic in this program goes to standard error and the checker compares standard output only, and main returns 0 on every path including the empty input, since a nonzero exit status is a failure however right the printed output looks.
Success Criteria
Your code must pass 10 test case(s) to complete this exercise. 3 hint(s) are available if you need help.
Sign in to track your progress
You can work on exercises as a guest, but sign in to track your progress and save your submissions.