Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will develop a C program to implement the \"malloc\", \"

ID: 3602546 • Letter: I

Question

In this assignment, you will develop a C program to implement the "malloc", "free", "calloc" and "realloc" functionalities. Instructions: (1) Check out the malloc tutorial from Dan Luu on how these functions can be implemented at: http://danluu.com/malloc-tutorial/ for your convenience, it is also appended in this file. The tutorial implements working versions of malloc, free, calloc and realloc and you can use that code as-is. (2) Write a driver function (i.e., main function) to demonstrate the usage and efficiency of these functions. The main 0 should have at least 10 calls each to malloc, calloc and realloc and at least 5 calls to free. Set up the function to print out the heap start/end addresses and also print out the memory leaks in your code. (3) your main task is to implement the exercises mentioned in the tutorial. These are also shown below: (a) Convert the single linked list implementation of the tutorial into a doubly linked list version; make changes to all the functions accordingly to work with the doubly linked list. (b) malloc is supposed to return a pointer "which is suitably aligned for any built-in type" Does our malloc do that? If so, why? If not, fix the alignment. Note that "any built-in type" is basically up to 8 bytes for C. (c) The implemented malloc is really wasteful if we try to re-use an existing block and we don't need all of the space. Implement a fünction that will split up blocks so that they use the minimum amount of space necessary (d) After doing (c), if we call malloc and free lots of times with random sizes, we'll end up with a bunch of small blocks that can only be re-used when we ask for small amounts of space. Implement a mechanism to merge adjacent free blocks together so that any consecutive free blocks will get merged into a single block. () The current implementation implements a first fit algorithm for finding free blocks. Implement a best-fit algorithm instead. ()Repeat Step (2) with this implementation to demonstrate usage o the functions and memory leakage. (5) Your code must use the set-up mentioned in this tutorial. Other online resources can be consulted but NOT copied. The tutorial mentions some other implementations for splitting/merging blocks that can only be consulted but not copied. To turn in: 1. You will need 4 files: (a) a header file with all the function prototypes, (b) a.c file with all the function definitions, (c) a .c file with the driver main O function and (d) a Make file to compile your code. Note that you only need to submit the improved versions of the functions and not the preliminary implementations from the tutorial. Additionally, include a text file mentioning which portions of your code are working and which portions don't and why

Explanation / Answer

Dear student,

I am providing you driver programs in C using malloc(),calloc() and relloc() system calls with free().

malloc(),calloc() and relloc() is used for dynamically allocating memory in heap and free() is used to free the dynamically allocated space.

In case ,if number of mwmory segment allocated through malloc(),calloc() and relloc() system calls is not freed using free(),then condition of memory leak occurs.

In your question above ,if we allocate 10 memory segement and erase only 5 dynamically allocated memort then other 5 unfreed memory space will cause memory leak.

On linux platform we generally use Valgrind tool to detect memory leak in our code.

1. Memory allocation using malloc

#include <stdio.h>

#include <stdlib.h>

int main()

{

int n, i;

int *ptr,

int sum = 0;

printf("Enter number of elements ");

scanf("%d", &n);

ptr = (int*) malloc(n * sizeof(int)); //memory allocated using malloc

if(ptr == NULL)

{

printf("Error! memory is not allocated through malloc");

exit(0);

}

printf("Enter elements of Array ");

for(i = 0; i < n; ++i)

{

scanf("%d", ptr + i);

sum =sum + *(ptr + i);

}

printf("Sum = %d", sum);

free(ptr); //here we are freeing memory allocated through malloc

return 0;

}

2. Memory allocation throgh calloc

#include <stdio.h>

#include <stdlib.h>

int main()

{

int n, i;

int *ptr;

int sum = 0;

printf("Enter number of elements of array");

scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int)); //here we are allocating memory using calloc

// Note:calloc initialises memory allocated through it to ZERO by default.

  

if(ptr == NULL)

{

printf("Error! memory not allocated trough calloc");

exit(0);

}

printf("Enter elements of array: ");

for(i = 0; i < n; ++i)

{

scanf("%d", ptr + i);

sum += *(ptr + i);

}

printf("Sum = %d", sum);

free(ptr);// here we are freeing memory allocated through calloc

return 0;

}

3.Memory reallocation through relloac

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *ptr;
int i ;
int n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);

ptr = (int*) malloc(n1 * sizeof(int)); //initially allocated memory through malloc

// printing address of previously allocated memory
for(i = 0; i < n1; i++)
printf("%u ",ptr + i);

printf(" Enter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
// printing address of newly allocated memory through realloc
for(i = 0; i < n2; i++)
printf("%u ", ptr + i);
return 0;
}

If you have any confusion ,feel free to comment.

Thanks !!!