Download presentation
Presentation is loading. Please wait.
Published bySharyl Burke Modified over 9 years ago
1
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers, and a bit More
2
Programming III Remember to Check – http://moodle.cis.fiu.edu http://moodle.cis.fiu.edu – http://users.cis.fiu.edu/~forte007/prog3/ http://users.cis.fiu.edu/~forte007/prog3/
3
Remember malloc calloc realloc free
4
Example (8.7) Storage Allocator
5
Storage Allocator Example Uses First Fit – Best fit will look for smallest chunk needed. – First fit looks for first space that has enough space If exact space found, it is unlinked and return to user If the block is too big, it is split – The proper amount is returned to user – Residual is left on the free list If not big enough, another chunk is obtain from OS and linked into the free list
6
Storage Allocator Example If space being free is adjacent to free block in either side – it is coalesced into single bigger block Most restrictive type is used – Some machines may be a double – And others long or int A free block contains – a pointer to the next block in the chain. – a record of the size of the block – free space If the block is too big, it is split – The proper amount is returned to user – Residual is left on the free list If not big enough, another chunk is obtain from OS and linked into the free list
8
Align, Size, and More Align field is never used. – Forces each header to be aligned on a worst-case boundary The size field is necessary because the blocks need to be contiguous – Why is not possible to use pointer arithmetic? The block contains one more unit – The header If programmer messes with reserved blocks, – list will be scrambled
9
Allocation (more) Variable base is used to get started – empty list if freep is NULL, it is the first call to malloc then a degenerate free list is created – Contains one block of size zero. – Points to itself Then List is search The free space is return to the user
11
malloc The function morecore obtains storage from OS – It is expensive. We want to get at least NALLOC units After setting the size field – morecore inserts the additional memory Unix system call is sbrk(n) – Returns a pointer to n more buts of storage – returns -1 if no space – -1 must be cast to (char *) to compared – This code is portable among machines that general pointer comparison is meaningful
13
Free Scans free list starting at freep looking for the place to insert free block – Either between two existing blocks or at one end of the list Pointers must be updated to point to correct places
15
Allocation Example While this allocation is machine dependent – illustrate the concepts This can be used in other situations – Hint (Homework) Suggested Exercises 8-6, 8-7,8-8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.