This project is optional. I will drop your lowest project grade in final grade calculations. If you do not submit this project your project component grade will be based on the review project and projects two and three.You may work alone or with one other person on this project. The other person must be a student in the class this semester. If you work with another person you and your partner will together make a joint submission (from either of your accounts - do not make two submissions). You must both document clearly in the code what code each of you wrote. You should each contribute equally to the project. If you do not, your grade may be adjusted to reflect how much work you actually contributed to the project.
The ranges of array indices is 0-31 for the stack and 0-127 for the heap.
The only values stored in memory locations are pointers, which can have as value a valid memory address (a value in the range 1-127) or null (the value 0).
You must have a way of indicating whether a memory cell is being used or is free. I suggest that you use some of the free bits in the representation of an unsigned int to encode this information. We can't just use the sign of the int, because null is represented as 0, and -0 is just 0. In other words, if we use the sign of the number to indicate whether it is occupied or not all nulls will either always be reclaimed or never be reclaimed.
Consider a different approach. An unsigned int occupies 32 bits on our systems, using CC to compile. A valid heap index only requires 7 bits (0000001 through 1111111). This means we have quite a few bits in an unsigned int that are not being used to represent a valid heap index. Let's use those bits to indicate the status of the memory cell.
How can you store more than one thing in a 32 bit string? You can use bit masks and bitwise operators to read and write individual bits or collections of bits. Consider the following code:
When we "and" two bit strings, the bits in the result that are 1 are those that are 1 in both of the two original bit strings. The mask has 1's in those bit positions that we want to read from the "cell" bit string. In the above example the "mask" has 1's only in the bit positions 0 through 3. In (cell & mask) all the bits in positions from 4 through 31 must be zero (since they are zero in the mask), but the bits in positions 0 through 3 have the same value they have in "cell". This shows how we can read particular bits of a longer bit string.#include <iostream> using std::cout; using std::endl; int main() { unsigned int cell = 65534; unsigned int mask = 15; cout << "mask is " << mask << endl; // 0000 0000 0000 0000 0000 0000 0000 1111 cout << "cell is " << cell << endl; // 0000 0000 0000 0000 1111 1111 1111 1110 cout << "cell & mask is " << (cell & mask); // 0000 0000 0000 0000 0000 0000 0000 1110 cout << endl; return 0; }The output is:mask is 15 cell is 65534 cell & mask is 14
How can we set bits in a representation? To turn them on we "or" with a bit-string with a 1 in the desired position:
To turn bits off we "and" with a the negation of the bit-string with a 1 in the desired position:#includeThe output isusing std::cout; using std::endl; int main() { unsigned int val = 7; unsigned int bit16 = 65536; unsigned int bit15 = 32768; unsigned int bit14 = 16384; unsigned int bit13 = 8192; unsigned int bit12 = 4096; unsigned int bit11 = 2048; unsigned int bit10 = 1024; unsigned int bit09 = 512; unsigned int bit08 = 256; unsigned int bit07 = 128; cout << "val is " << val << endl; cout << "bit10 is " << bit10 << endl; cout << "val | bit10 is " << (val | bit10); cout << endl; return 0; } val is 7 bit10 is 1024 val | bit10 is 1031
#includeThe output isusing std::cout; using std::endl; int main() { unsigned int val = 1031; unsigned int bit16 = 65536; unsigned int bit15 = 32768; unsigned int bit14 = 16384; unsigned int bit13 = 8192; unsigned int bit12 = 4096; unsigned int bit11 = 2048; unsigned int bit10 = 1024; unsigned int bit09 = 512; unsigned int bit08 = 256; unsigned int bit07 = 128; cout << "val is " << val << endl; cout << "bit10 is " << bit10 << endl; cout << "val & ~bit10 is " << (val & ~bit10); cout << endl; return 0; } val is 1031 bit10 is 1024 val & ~bit10 is 7
pop which first decrements the stack pointer and then returns the contents of the location currently pointed at by the stack pointer.
push(addr) which assigns addr to the location currently pointed at by the stack pointer, and then increments the stack pointer.
The null pointer must be represented by the value 0.allocate which assigns a null pointer to each component of the cons cell.new()
allocate which assigns the value of first to the first component of the cons cell and the value of second to the second component of the cons cell.new(first, second)
These methods can also be defined directly on the heap, in which case they will be defined as follows:getFirst() which returns the contents of the first component of the cons cell
setFirst(addr) which assigns addr to the first component of the cons cell
getSecond() which returns the contents of the second component of the cons cell
setSecond(addr) which assigns addr to the second component of the cons cell
getFirst(index) which returns the contents of the first component of the cons cell whose starting address is index
setFirst(index, addr) which assigns addr to the first component of the cons cell whose starting address is index
getSecond(index) which returns the contents of the second component of the cons cell whose starting address is index
setSecond(index, addr) which assigns addr to the second component of the cons cell whose starting address is index
Write down questions that you have, and bring them to office hours (mine or the TAs'), or send them to cse-250.
The PP4 directory must include only your source code (declaration and implementation) files as well as your makefile and any required documentation.
Your submission must not include object files,
executable files or any files whose filenames begin with `#' or end in
`~'. You also must not include the SunWS_cache
directory and its contents in your submission.
Use the zip utility to create a file named
`PP4.zip'. From the directory containing the `PP4'
directory, issue the command
zip -r PP4 PP4
Submit only the resultant `PP4.zip' file.
Use the submit_cse250 command to submit your work.