Question
Write a linked list memory checker with C language and test the code. UPDATE: When I first created this assignment, I asked you to work
Write a linked list memory checker with C language and test the code.
UPDATE:
When I first created this assignment, I asked you to work the the reallocf and valloc functions in addition to calloc, free, malloc ard realloc. The first two functions are not standard so, I have removed them from the assignment requirements.
Introduction.
For this assignment, you will be writing a set of functions and a header file that analyzes the dynamic memory used by a program. You will not be writing the main file. Your program will check each calloc, free, malloc, realloc, reallocf, and valloc. It will keep track of all the addresses of allocated memory, until they are freed. If, at any point, a memory address is freed that was not allocated, an error message will be printed. At the end of the program, an error message will be printed for each memory address that was allocated but not freed.
Include
The test programs that will be used to try out your functions and header file will be included in a single *.c file. This file will be compiled with the, "-include memcheck.h" option and the file "memcheck.c" will be added to the compile inputs. Of course, we will use the usual "-Wall -ansi -pedantic" options. You will provide (by uploading to git) the files "memcheck.h" and "memcheck.c". Your "memcheck.h" file will include 7 #define directives that replace the calloc, free, malloc, realloc, reallocf, valloc and main functions in the test program with your own versions of these functions called memcheck_calloc, memcheck_free, memcheck_malloc, memcheck_realloc, memcheck_reallocf, memcheck_valloc, and memcheck_main, respectively, as well as the prototypes for your own memcheck_* functions.
Replacement Functions
You will include a main function in memcheck.c which will call the memcheck_main function (which is the renamed version of the main in the test program). Before calling memcheck_main, your main function will initialize a variable that will form the basis of a linked list. After calling memcheck main, your main function will print out error messages for any memory address that was allocated but not freed.
You will write functions: memcheck_calloc, memcheck_malloc, memcheck_realloc, memcheck_reallocf, and memcheck_valloc that add an item to the head of the linked list each time the function is called.
You will write the function, memcheck_free, which will search through the linked list in linear order, searching for an item in the list that matches the memory address that is being freed. If it finds the address, it should remove it from the list and actually free the memory. If it is not found it should print an error message (see below), but not free the memory.
Compiler Macros
The compiler supports two macros: __FILE__, __LINE__. (Note, it's hard to see here, but both of these start and end with a double underscore (_). Each of these macros is expanded, at compile time, by the filename and line number on which it occurs. These macros are processed after #define statements.
Error messages
Your program should print out the following error messages:
For trying to free a memory address that was not allocated:
"memcheck error: attempting to free memory address %p in file \"%s\", line %d. "
For leaving an allocated memory address unfree:
"memcheck error: memory address %p which was allocated in file \"%s\", line %d, was never freed "
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started