B.YASWANTH (RA211030010262) : Department of Networking and Communications

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

A MINI PROJECT REPORT

21CSC202J -OPERATING SYSTEMS


Submitted by
B.YASWANTH[RA211030010262]

P.LAXMI Narayana[RA211030010258)

A.HARSHA[RA211030010249]

Under the guidance of


Ms.V. Vijayalakshmi
Assistant Professor, Department of Networking and Communication
in partial fulfilment of the requirements for the

degree of

M.TECH INTEGRATED
COMPUTER SCIENCE
ENGINEERING
with specialization
in CYBER
SECURITY

DEPARTMENT OF NETWORKING AND COMMUNICATIONS


FACULTY OF ENGINEERING AND TECHNOLOGY SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY
KATTANKULATHUR – 603 203
MAY 2023
1
BONAFIDE CERTIFICATE

Certified that this project report titled “MEMORY ALLOCATION” is the Bonafide work of
“B.YASWANTH [RegNo:RA2211030010262],P . L A X M I N A R A Y A N A [Reg No:RA2211030010258]
A.HARSHA[Reg No:RA2211030010146]” who carried out the project work under my supervision.
Certified further, that to the best of my knowledge, the work reported herein does not form part of
anyother thesis or dissertation based on which a degree or award was conferred on an earlier occasion
for this or any other candidate.

Ms.V. Vijayalakshmi Dr. Annapurani Panaiyappan. K

Assistant Professor HEAD OF THE DEPARTMENT


Department of Networking Department of Networking and
andCommunications Communications

SIGNATURE OF INTERNAL SIGNATURE OF


EXTERNAL EXAMINER EXAMINER

2
TABLE OF CONTENTS
S.NO CONTENTS PAGE NO

1. OBJECTIVE 4

2. ABSTRACT 5

3. INTRODUCTION 6

4. PROBLEM STATEMENT 7

5. LIMITATIONS 8

6. BLOCK DIAGRAM 9

7. PROGRAM 10-11

8. OUTPUT 12

9. CONCLUSION 13

10. REFERENCES 14

3
OBJECTIVE

The objective of the provided C code is to implement a custom


memory management system that provides functions for
memory allocation, reallocation, and deallocation. The code
aims to create a wrapper around the standard memory allocation
functions (malloc, realloc, free) to keep track of allocated
memory blocks and provide more control and insight into
memory usage.

Key objectives of the code:

1. *Custom Memory Management*: The code defines custom


memory management functions: my_malloc, my_realloc, and
my_free, which mirror the functionality of malloc, realloc, and
free, respectively.

2. *Memory Tracking*: It maintains a linked list of allocated


memory blocks using a Block structure. Each allocated block's
size and reference are stored in the linked list.

3. *Error Handling*: The code includes error handling for


memory allocation and reallocation failures, printing an error
message when memory allocation fails.

4. *Memory Cleanup*: The my_free function not only frees


memory but also removes the corresponding Block structure
from the tracking list to prevent memory leaks. The my_free_all
function releases all allocated memory blocks, ensuring proper
cleanup when the program exits.

5. *Testing*: The code provides a simple example in the main


function to demonstrate the usage of these custom memory
management functions by allocating, reallocating, and freeing
memory for an integer pointer.

4
ABSTRACT

The provided C code presents a custom memory management system that


encapsulates standard memory allocation functions (malloc, realloc, free)
to offer enhanced memory tracking, error handling, and efficient memory
cleanup. Key elements of this code include:

- *Custom Memory Functions*: The code defines custom memory


management functions: my_malloc, my_realloc, and my_free, designed
to closely mimic the functionality of standard memory functions. These
custom functions are intended to provide a higher level of control and
monitoring over allocated memory.

- *Memory Tracking*: The code incorporates a linked list data structure,


using the Block structure, to keep track of allocated memory blocks. Each
Block includes information about the size of the allocated memory and a
reference to the next block, facilitating more granular management of
memory resources.

- *Error Handling*: Robust error handling mechanisms are in place. If


memory allocation or reallocation fails, the code prints an error message
to the standard error stream (stderr). This ensures that allocation failures
are properly handled, enhancing the program's reliability.

- *Memory Cleanup*: The my_free function not only deallocates


memory but also effectively removes the corresponding Block structure
from the tracking list. This safeguards against memory leaks and ensures
that resources are efficiently released.

- *Comprehensive Testing*: The code includes a practical example in the


main function to showcase the application of custom memory
management functions. It demonstrates the allocation, reallocation, and
deallocation of memory for an integer pointer, offering a real-world
context for the code's usage.

5
INTRODUCTION:

It defines functions for allocating, reallocating, and freeing memory,


along with a linked list data structure to keep track of allocated
memory blocks. Here's an overview of the main components and
their functions

1. Block Structure:
- Block is a custom structure used to keep track of allocated
memory blocks.
- It contains two members: size to store the size of the allocated
memory block and next to maintain a linked list of allocated blocks.

2. allocated_blocks Linked List:


- allocated_blocks is a global pointer that represents a linked list of
allocated memory blocks.
- It keeps track of all memory blocks allocated by the custom
allocation functions.

3. my_malloc Function:
- Custom memory allocation function that mimics the behavior of
the standard malloc function.
- It allocates a block of memory using malloc, creates a
corresponding Block structure to track it, and adds the structure to
the linked list.

4. my_realloc Function:
- Custom memory reallocation function that mimics the behavior
of the standard realloc function.
- It reallocates memory using realloc, updates the size in the
corresponding Block structure, and maintains the linked list.

The main function demonstrates the usage of these custom memory


allocation functions by allocating an integer, reallocating it to hold
two integers, and then freeing the allocated memory. Finally, it calls
my_free_all to release all remaining allocated memory.

6
PROBLEM STATEMENT:

*Resource Leak Risk*: The code maintains a linked list


(allocated_blocks) to track allocated memory blocks, but
it doesn't handle the case where the user directly calls
free() on a memory block. This can lead to a resource
leak because the corresponding Block structure is not
removed from the list.

Memory Block Search*: In both my_realloc and my_free


functions, the code searches for the corresponding Block
structure by comparing pointers. This is not a reliable
way to identify memory blocks, as it assumes that the
memory address of the structure matches the original
pointer, which may not always be the case. It would be
safer to use a unique identifier or tag associatedwith each
block

*Redundant Error Handling*: The code contains


repetitive error handling code for memory allocation
failures, which can be refactored to reduce redundancy
and improve maintainability.
.

7
LIMITATIONS:

a custom memory allocation and management system in C, but


it has several limitations and potential issues:

1. *Inefficient Memory Management*: The code uses a linked


list (allocated_blocks) to keep track of allocated memory blocks.
While this allows for tracking allocated blocks, it is not the most
efficient way to manage memory, especially if a large number of
allocations and deallocations are performed.

2. *Performance Overhead*: The use of a linked list to track


memory blocks can result in slower allocation and deallocation
operations, especially when searching for the corresponding
Block structure for a given memory pointer.

3. *Incomplete Error Handling*: The code lacks detailed error


handling. While it checks for allocation and reallocation
failures, it doesn't provide meaningful error messages or proper
recovery mechanisms. This makes debugging and
troubleshooting more challenging.

4. *Inadequate Handling of Custom Memory Operations*: The


code does not address scenarios where the user directly calls
standard memory functions like free() on an allocated block,
potentially leading to resource leaks.

8
BLOCK DIAGRAM:

+----------------------------------+
| Main Program (main function) |
+----------------------------------+
|
v
+----------------------------------+
| Custom Memory Allocation Functions |
+----------------------------------+
|
v
+----------------------------------+
| Data Structures |
| - Block structure |
| - allocated_blocks (linked list)|
+----------------------------------+
|
v
+----------------------------------+
| Standard Library Memory Functions|
| - malloc, realloc, free |
+----------------------------------+
|
v
+----------------------------------+
| Error Handling |
| - Allocation and reallocation |
+----------------------------------+
|
v
+----------------------------------+
| Print Statements |
+----------------------------------+

9
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

// Structure to keep track of allocated memory blocks


typedef struct Block {
size_t size;
struct Block* next;
} Block;

Block* allocated_blocks = NULL; // A linked list to keep track of


allocated blocks

// Function to allocate memory


void* my_malloc(size_t size) {
// Use the standard library function to allocate memory
void* ptr = malloc(size);

if (ptr == NULL) {
// Handle allocation failure
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}

// Create a new Block structure to track the allocated memory


Block* block = (Block*)malloc(sizeof(Block));
if (block == NULL) {
// Handle allocation failure
free(ptr);
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}

block->size = size;
block->next = allocated_blocks;
allocated_blocks = block;

return ptr;
}

// Function to reallocate memory


void* my_realloc(void* ptr, size_t size) {
// Check if the original pointer is NULL
10
if (ptr == NULL) {
return my_malloc(size);
}

// Use the standard library function to reallocate memory


void* new_ptr = realloc(ptr, size);

if (new_ptr == NULL) {
// Handle reallocation failure
fprintf(stderr, "Memory reallocation failed\n");
return NULL;
}

// Update the size in the corresponding Block structure


Block* current = allocated_blocks;
while (current != NULL) {
if (current == (Block*)ptr) {
current->size = size;
break;
}
current = current->next;
}

return new_ptr;
}

// Function to free memory


void my_free(void* ptr) {
if (ptr == NULL) {
return;
}

// Use the standard library function to free memory


free(ptr);

// Find and remove the corresponding Block structure from the list
Block* current = allocated_blocks;
Block* previous = NULL;

while (current != NULL) {


if (current == (Block*)ptr) {
if (previous != NULL) {
previous->next = current->next;

11
} else {
allocated_blocks = current->next;
}
free(current);
break;
}

previous = current;
current = current->next;
}
}

// Function to release all allocated memory


void my_free_all() {
while (allocated_blocks != NULL) {
Block* current = allocated_blocks;
allocated_blocks = current->next;
free(current);
}
}

int main() {
int* int_ptr = (int*)my_malloc(sizeof(int));
*int_ptr = 42;
printf("Value: %d\n", *int_ptr);

int_ptr = (int*)my_realloc(int_ptr, sizeof(int) * 2);


printf("Value after reallocation: %d\n", *int_ptr);

my_free(int_ptr);
my_free_all(); // Release all allocated memory

return 0;
}

12
OUTPUT:

13
CONCLUSION:
a custom memory management system implemented in C. It
allows you to allocate, reallocate, and free memory using
custom functions (my_malloc, my_realloc, my_free) while
also keeping track of the allocated memory blocks. Here's a
summary of the code:

1. *Memory Allocation and Tracking:*


- The code defines a Block structure to keep track of
allocated memory blocks. Each Block contains information
about the size of the allocated memory and a pointer to the
next block.
- A linked list named allocated_blocks is used to maintain
a record of all allocated memory blocks.

2. *Custom Memory Allocation Functions:*


- my_malloc(size_t size): Allocates memory using the
standard library malloc function and adds a new Block to
the linked list to track the allocation.
- my_realloc(void* ptr, size_t size): Reallocates memory
using the standard library realloc function and updates the
size in the corresponding Block structure.
- my_free(void* ptr): Frees memory using the standard
library free function and removes the corresponding Block
structure from the linked list.

14
REFERENCES:

Here are some references that you can look up for more information
on memory allocation:

Wikipedia:
https://en.m.wikipedia.org/wiki/C_dynamic_memory_allocation
Greeks for greeks:
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-
malloc-calloc-free-and-realloc/

15

You might also like