COSC 2P91
Dynamic allocation Week 4a
Brock University
Brock University (Week 4a) Dynamic allocation 1 / 22
COSC 2P91 Dynamic allocation Week 4a Brock University Brock - - PowerPoint PPT Presentation
COSC 2P91 Dynamic allocation Week 4a Brock University Brock University (Week 4a) Dynamic allocation 1 / 22 Before we begin... Two quick addenda to last week... Brock University (Week 4a) Dynamic allocation 2 / 22 Pointers to local
Brock University (Week 4a) Dynamic allocation 1 / 22
Brock University (Week 4a) Dynamic allocation 2 / 22
◮ A static member will obviously still exist ◮ e.g. to a static array (say to use as an intermediate buffer). It’s a little
Brock University (Week 4a) Dynamic allocation 3 / 22
Brock University (Week 4a) Dynamic allocation 4 / 22
◮ Or we may simply want a ragged array
◮ e.g. lists, trees, etc.
Brock University (Week 4a) Dynamic allocation 5 / 22
◮ Because the same function is used for allocation of any type, it returns
◮ If the latter seems unlikely, consider structs (particularly linked
◮ It can be easy to accidentally exceed the allocated bounds of an array ◮ Similarly, remember to definitely allocate, and not simply declare the
⋆ (cough) Brock University (Week 4a) Dynamic allocation 6 / 22
Brock University (Week 4a) Dynamic allocation 7 / 22
Brock University (Week 4a) Dynamic allocation 8 / 22
◮ One for the number of elements ◮ One for the size of each element Brock University (Week 4a) Dynamic allocation 9 / 22
◮ If you had more than one pointer to the array, then they can very easily
◮ This is one of the reasons why you may wish to try to avoid realloc
Brock University (Week 4a) Dynamic allocation 10 / 22
Brock University (Week 4a) Dynamic allocation 11 / 22
◮ After all, if we had enough memory to allocate it, and if it was ever
◮ Try not to putz around with this one (particularly when using pointer
Brock University (Week 4a) Dynamic allocation 12 / 22
◮ Example time! Brock University (Week 4a) Dynamic allocation 13 / 22
Brock University (Week 4a) Dynamic allocation 14 / 22
Brock University (Week 4a) Dynamic allocation 15 / 22
◮ e.g. comparators, operators, intersections, etc.
Brock University (Week 4a) Dynamic allocation 16 / 22
◮ Besides that, one could combine this concept with what we saw in our
◮ If you want a struct to be able to operate on itself, via its own
Brock University (Week 4a) Dynamic allocation 17 / 22
Brock University (Week 4a) Dynamic allocation 18 / 22
◮ Simply create code for a single type of stack — e.g. int ◮ Write separate stack code for multiple types ◮ Employ void pointers since the data itself doens’t affect the push/pop
⋆ Of course, this puts the burden of type safety onto the client Brock University (Week 4a) Dynamic allocation 19 / 22
◮ You may wish to consider writing a comparator for whatever type it is
◮ You probably won’t be able to completely guard against improper use,
⋆ That is, write separate functions to handle everything, so that all
Brock University (Week 4a) Dynamic allocation 20 / 22
◮ Are there any conventions we should use? Brock University (Week 4a) Dynamic allocation 21 / 22
Brock University (Week 4a) Dynamic allocation 22 / 22