User Tools

Site Tools


notes:fall2024:projects:msi2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:fall2024:projects:msi2 [2024/09/21 01:23] – [append() function] cmazzaranotes:fall2024:projects:msi2 [2024/09/27 00:23] (current) – [rmlist() function] jmerri10
Line 36: Line 36:
 List *obtain (List *, Node **); List *obtain (List *, Node **);
 </code> </code>
 +For obtain the goal is to get the current node and remove it from the list so that you can either freely manipulate it or simply use it as an argument for rmnode().\\ 
 +Things to be mindful when creating this function are:
 +  * Is the list made up of just that node?
 +  * Does myList->start == that node?
 +  * Does myList->end == that node?
 +  * Make sure whatever is before that node has its next assigned (*object)->next;
 +  * Assign (*object)->next to be NULL
 +  * 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\\ 
 +Notice the type of the function is of List *, this means you will **return a value**!
  
 =====clearlist() function===== =====clearlist() function=====
Line 42: Line 52:
 </code> </code>
  
 +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 47: Line 58:
 </code> </code>
  
 +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/overflow. This is also why it is important to clear your list before reinitializing/resetting your game objects. Clearing unused memory is a good habit to develop!
notes/fall2024/projects/msi2.1726881790.txt.gz · Last modified: 2024/09/21 01:23 by cmazzara