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/19 00:57] – [pointer arithmetic] cburling | 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 you can't simply add the size of the element to the address to reach the next. While it is faster to do so, this project is about Lists and thus we will do it that way.\\ | + | With Lists and Nodes, unlike Arrays since elements are not stored contiguously, you can't simply add the size of the element to the address to reach the next. While it is faster to do it that way, this project is about Lists and thus we will do it this way.\\ |
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\\ | 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\\ |