This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:fall2024:projects:msi2 [2024/09/25 21:41] – [obtain() function] cburling | notes:fall2024:projects:msi2 [2024/09/27 00:23] (current) – [rmlist() function] jmerri10 | ||
---|---|---|---|
Line 43: | Line 43: | ||
* Make sure whatever is before that node has its next assigned (*object)-> | * Make sure whatever is before that node has its next assigned (*object)-> | ||
* Assign (*object)-> | * Assign (*object)-> | ||
+ | * Note that the parameter is Node** so you want to properly de-allocate for what you end up passing in | ||
You'll also want a variable such as Node* theForsaken to be assigned that node prior to using obtain as you don't want to lose connection to that node\\ | You'll also want a variable such as Node* theForsaken to be assigned that node prior to using obtain as you don't want to lose connection to that node\\ | ||
- | Notice the type of the function is of List *, this means you will return a **value**! | + | Notice the type of the function is of List *, this means you will **return a value**! |
=====clearlist() function===== | =====clearlist() function===== | ||
Line 51: | Line 52: | ||
</ | </ | ||
+ | The purpose of the clearlist function is to have the list emptied of nodes before the list itself is destroyed. A not-bad idea would be to have the list cycle through all still remaining elements, and obtain / remove them until the list itself is empty. The end product should only have first and last, both set to NULL. Keep in mind that the primary use of this function will be inside the rmlist function. | ||
=====rmlist() function===== | =====rmlist() function===== | ||
<code c> | <code c> | ||
Line 56: | Line 58: | ||
</ | </ | ||
+ | With the list now clear, it is time to invoke the rmlist function. This function is simple, and very similar to how rmnode works. Simply call the clearlist function, and then free the memory for which the list was occupying. The last step is to set the now empty, non-memory-occupying list to NULL and return. | ||
+ | It is necessary to remove the list at the end of your game loop to manage memory properly and avoid memory leaks. If you do not remove the list, memory will not be freed and any memory occupied by the nodes you’ve added to the list will accumulate and lead to memory leak/ |