This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:fall2024:projects:msi1 [2024/09/18 21:23] – [space invaders] amelvil2 | notes:fall2024:projects:msi1 [2024/09/19 03:39] (current) – [malloc] bpatrice | ||
---|---|---|---|
Line 3: | Line 3: | ||
The primary difference between the last and current project is that instead of using arrays, linked lists are to be employed instead. Essentially, | The primary difference between the last and current project is that instead of using arrays, linked lists are to be employed instead. Essentially, | ||
=====malloc===== | =====malloc===== | ||
+ | You start with nothing, then there is the great sneeze! Or in this case the malloc! At this point you should all know that malloc is memory allocation. In the Joe O classes you may be familiar with the term //Newed Up//? It's essentially that. | ||
< | < | ||
- | start = (Sprite *) malloc( sizeof (Sprite)); | + | Sprite* Node |
+ | Node = (Sprite*)malloc(sizeof(Sprite)); | ||
</ | </ | ||
- | When making | + | Congratulations you now have a Node! You might also find it useful |
+ | |||
+ | Also, remember that whenever you allocate memory(malloc) you are also responsible for freeing that memory at the end of your program. | ||
+ | You can do this by using | ||
+ | < | ||
+ | free(Node); | ||
+ | </ | ||
=====pointer arithmetic===== | =====pointer arithmetic===== | ||
+ | With Lists and Nodes, unlike Arrays since elements are not stored contiguously, | ||
+ | Using Lists requires Structs but not any Struct will do, these Structs require a fancy maneuver called **next** whose type will be a pointer of the same name as the Struct\\ | ||
+ | *Example\\ | ||
+ | < | ||
+ | struct Node { | ||
+ | int data; | ||
+ | Node* next; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Using the **next** property and assigning it to point at a new Node //" | ||
+ | |||
+ | < | ||
+ | Struct* current = NULL; | ||
+ | current = myList-> | ||
+ | while( current != NULL ) { | ||
+ | ... | ||
+ | current = current-> | ||
+ | } | ||
+ | </ | ||
+ | This will get you through the entire length of your list if that were to be some objective of yours | ||
=====node struct===== | =====node struct===== | ||
To make your node struct, you need to take whatever you are making into a list, in this case your Sprite/ | To make your node struct, you need to take whatever you are making into a list, in this case your Sprite/ |