void dumpHeap(Memory* heap, int size); // Purpose: display the contents of the simulated heap // to stderr. // The size input parameter (which must be a multiple // of four) defines how many Memory cells we display // on a given row of output.The
dumpHeap() function prints,
to stderr, a visualization of the current
contents of the simulated heap. Each memory cell
in the simulated heap will correspond to one character. All
free memory (the memory that is not used in an allocated
block) will have a blank space (' '). For
memory cells that are used in an allocated block
'H'
'A'
'C'
Memory location zero in the simulated heap is special and
will be represented by the character 'X'.
If the size of the heap is not a multiple of the number
of memory cells to display in each row, then fill the remaining
spaces of the last row with '#' characters.
For example, suppose we have
Memory* heap = initializeHeap(20); charPointer array1 = charMalloc(heap, 2); charPointer array2 = charMalloc(heap, 5); dumpHeap(heap, 8);The output (to stderr) from the dumpHeap() function should look exactly like
+--------+ |XHACCAHA| |CCCCCA | | ####| +--------+The memory should have a "frame" drawn around it using the plus (+), minus (-) and pipe (|) characters. The first memory cell location (heap[0]) is the top left hand corner of the memory diagram. Given any memory cell i, the next memory cell is either printed immediately to the right of i, or, if i is at the end of a given row, then the next memory cell is the first location (far left) of the next row. Since there are 20 memory cells in our simulated heap and the row size is 8, we pad the end of the memory diagram with 4
'#'
characters.
If we then call charFree(heap, array1); and
then dumpHeap(heap,4), the output from the
dumpHeap() function should look exactly like
+----+ |X | | HA| |CCCC| |CA | | | +----+