|CS 321 Spring 2013 > Assignment 4|
Assignment 4 is due at 5 p.m. Tuesday, April 4. It is worth 25 points.
using the subject
mapalloc.cpp, from Exercise A, and
randacc.cpp, from Exercise B. These files (or a single archive file containing them) should be attached to your e-mail message.
In this exercise, you will write simple memory allocation &
deallocation functions, using
mmap to get the memory.
Write a C++ package to do memory allocation and freeing,
much like the standard library functions
mapFree. Prototype them in the header
mapalloc.h, and implement them in the source file
std::size_t, indicating a number of bytes to be allocated. It returns a
(void *). The function attempts to allocated a block of (at least) the given size. The return value is a pointer to the allocated block, if the allocation was successful; otherwise, it is
(void *), which is a pointer to a block previously allocated with
mapAlloc. It returns nothing. The function deallocates the given block.
mmap; deallocation must be done using
and the result is notchar * p = (char *)mapAlloc(n);
NULL, then the bytes with addresses
p+n-1must be allocated memory with both read and write access. Furthermore, successive calls to
mapAllocmust return pointers to distinct, nonoverlapping blocks.
mapFreemay assume that it is given a pointer that was previously returned by
mapAlloc, and that
mapFreewill never be called twice with the same pointer. So you do not need to test for these conditions.
munmaptakes the size of the block to deallocate, as a parameter. However,
mapFreeis not given the size of the block. So you will need to store this information somewhere. How you store it, is up to you, but be intelligent about it.
In this exercise, you will write a program
that does random-access file I/O
Write a complete C++ program to do random-access I/O on a file. The file will contain 100 records, each holding an integer. The user is repeated allowed the choice of reading and printing the value in a specified record, or writing a user-provided value to a specified record. Data written by the program should be available for reading the next time the program is executed.
mmap. It must be random-access, meaning that the program computes the location of a record and reads/writes there, without reading previous records.
mmapshould be available under Cygwin. Alternatively, you can write your code using
NetRunwill not allow the file access required in Exercise B.