Ask Programming Language Expert

1. Create text a file with the name "myemail" that has a single line in it, consisting of your email address. Something like "[email protected]" (or your hotmail or gmail or hellokittymail or ...). Do not include the quotes in the name of the file or in your email address. This must be a plain text file (with no extension).
The results of the correctness tests will be emailed to the email address that you put in this file.

In this assignment, you will simulate a memory management system for the heap in a C program. You will also implement pointer arithmetic and simulate arrays.

1. Basic Heap: Each memory location in our model of the RAM will be an instance of type Memory:
2. typedef union
3. Memory_u Memory;
4. union Memory_u{
5. char character;
6. unsigned int size;
7. unsigned long address;
8. };

In each memory location of our simulated heap, which we'll call a memory cell, we can either store actual data (the character field) or other information that is needed for the dynamic memory management.
Each time we allocate some memory on our simulated heap, we need to use at least four (4) consecutive memory cells:

1. The first memory cell uses the size field to say how many data memory cells are allocated (we'll call this the head memory cell for our block of allocated memory);

2. The next memory cell uses the address field to indicate where the head memory cell is for the block of allocated memory that occurs before this current block in the simulated heap. If there are no blocks allocated that are located before this block, the address field will be zero (start of the heap and forbidden memory location);

3. The next one or more memory cells (defined by the head memory cell) are the actual allocated memory cells for the data (the characters);

4. The last memory uses the address field to indicate where the next free memory location in the simulated heap is. Since each memory allocation needs at least four consecutive memory cells, the next free memory location must have at least four consecutive free memory cells.

Our simulated heap will simply be an array of Memory in the actual heap. Array index zero (0) will correspond to memory address zero in our simulated heap and is off limits (to read or prepare) just as memory location zero (NULL) is off limits in your normal programs.
You will implement all of the functions found in nonstdlib.h (found here) that are not already defined in nonstdlib.c (found here). These functions include

Memory* initializeHeap(unsigned long size);
// Purpose: allocate memory on the actual heap for our
// simulated heap. Returns a pointer to the first
// memory location that we have reserved for our
// simulated heap
// (This function is partially defined for you )
// (you can add code to it, but do not change )
// (the last line of it in the .c file )

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.
// (A more precise description of the output
// is given here.)
typedef unsigned long charPointer;
charPointer charMalloc(Memory* heap, unsigned int size);
// Purpose: allocates memory in the heap for size
// chars.
// Returns the memory location in the simulated heap
// where the head memory cell for the allocated
// block of memory is. (memory location is heap array index)
// Returns 0 if allocation failed because there was
// no place in the heap for this requested block of memory

bool charFree(Memory* heap, charPointer address);
// Purpose: frees the memory allocated in the heap
// for the block at location address
// (memory locations in the simulated heap
// correspond to array indices in the heap)
// Function does nothing if this memory location
// has already been freed (and not allocated again)
// Returns false if the memory address has not been
// the return value of a call to charMalloc().
// Returns true otherwise.

In order to implement these functions, you will need to maintain a table of some sort. This table will need to keep track of which memory locations in the simulated heap are currently allocated and which have ever been allocated. You should use a global data structure in nonstdlib.c for this (so that all your functions can always access and modify this table).
When allocating memory, we will use a simple technique of simply allocating memory in the next available space in the heap after the most recent allocation. (This will be elaborated on in Thursday's lecture; information will be added here after the lecture for those that miss the class.)

Add the files nonstdlib.h and nonstdlib.c to your tar file. Make sure you add an appropriate header guard to the .h file. Your nonstdlib.c file should not have a main() function in it.

2. Arrays: In array.h (found here) there are two function prototypes for reading and writing elements in an array of chars (a charPointer instance). These array functions perform bounds checking on the index values supplied. You will implement these functions in array.c.
Be sure that you are accessing the correct memory cells where the characters are located.
For ex, code using an array might look like

Memory* heap = initializaHeap(100000);
charPointer array = charMalloc(heap, 10);
setChar(heap, array, 2, 'q');
char c;
if( getChar(heap, array, 2, &c) )
printf("character=%c\n", c);
charFree(heap, array);
which creates the heap, allocates an array of 10 chars, sets array[2] = 'q', puts array[2] into c, prints c, and then frees the memory allocated for array.

Add the files array.h and array.c to your tar file. Make sure you add an appropriate header guard to the .h file. Your array.c file should not have a main() function in it.

3.Main: prepare a main.c file that have a main function to demonstrate your dynamic memory and array usage. Your program should accept two command line arguments: the first will determine the size of your heap (number of Memory cells) and the second will determine the number of columns of memory cells when using the dumpHeap() function. Your code should call dumpHeap().
For ex, if main is the executable of your program then you would run your program from the shell as ./main 10000 8 to generate a heap with 10000 memory cells, and when printing the heap to stderr, it is displayed with 8 memory cells on each row.
Add the file main.c to your tar file. Be sure to include a Makefile which creates the executable main by linking the object code for main, nonstdlib and array.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9932

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As