Pointers are four bytes and pages are 4K. Here’s the heap. Malloc() and free() have been implemented as described in the last class. The head of the free list is (0x58a00
) (And if you don’t remember, the definition of the free list structs are to the right).
1
2
3
4
5
struct flist {
int size;
struct flist *flink;
struct flist *blink;
};
You’ll find the printout that I gave you to be helpful.
Please answer the following questions:
Question 1: How many bytes are in the first node (or memory chunk) on the free list?
0x30
or 48
Question 2: What is the memory address of the second node on the free list?
0x589a0
Question 3: How many bytes are in the second node (or memory chunk) on the free list?
0x10
or 16
Question 4: How many nodes are there on the free list?
Question 5: How many allocated chunks are there (memory chunks that have been malloc’d, but not freed)?
Question 6: If I call sbrk(0), what will it return (It is not 0x58a60)?
0x58a58
. If our page sizes are 4K, thne the addresses up to 0x58fff
will not seg fault, but they are not officially allocated to our process yet.
To explain this question, first, identify the nodes on the free list:
0x58a00
and is 0x30
= 48 bytes in size. Why 0x30
? Because the first four bytes are the size. The next four bytes are flink, so the next node on the free list is 0x589a0
0x589a0
and is 0x10
= 16 bytes in size. Its flink value is 0x589d0
, so that's the next node on the free list.0x589d0
and is 0x18
= 24 bytes in size. Its flink value is 0x00000
, so that's the end of the free list.That gives you the answers to questions 1-4. Now, to answer the rest of the question, it's helpful to differentiate the free memory from the allocated memory. I've done that below by coloring the free memory blue:
The allocated memory will be the memory in the black font. The first 4 bytes of a chunk will be the chunk's size. So:
0x589b0
and Size = 0x20
or 32 bytes. Therefore, the chunk goes right up to the free chunk at 0x589d0
.0x589e8
and Size = 0x18
or 24 bytes. Therefore, the chunk goes right up to the free chunk at 0x58a00
.0x58a30
and Size = 0x10
or 16 bytes. This means that the next chunk starts at 0x58a40
.0x58a40
and Size = 0x18
or 24 bytes. This means that the next chunk starts at 0x58a58
. Or not -- because there's not a valid size there, and the heap "ends" after 0x58a5c
. So this is the last chunk, and what we're printing are eight bytes that are on the same page, but have not been assigned to the process by the operating system yet. This clues us into the answer to the last question.