0% found this document useful (0 votes)
135 views

KT 24403 Operating System Lab 9: Memory Allocation

The document discusses different dynamic memory allocation techniques in C including malloc, calloc, realloc and free. It also provides examples of using these functions and describes two memory allocation strategies - first fit and best fit - explaining how to implement a memory allocation program using each strategy.

Uploaded by

Mae X
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

KT 24403 Operating System Lab 9: Memory Allocation

The document discusses different dynamic memory allocation techniques in C including malloc, calloc, realloc and free. It also provides examples of using these functions and describes two memory allocation strategies - first fit and best fit - explaining how to implement a memory allocation program using each strategy.

Uploaded by

Mae X
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

KT 24403 Operating System

Lab 9: Memory Allocation


Dynamic Memory Allocation
• allocation of memory until run time
• let the program decide how much memory it needs as it runs
• stdlib.h
malloc (memory allocation)
• dynamically allocate a large block of memory of specified size.
• returns a pointer that can be cast to any data type.
• If allocation fails (insufficient space), returns a NULL pointer.
• Syntax:
• ptr = (type*) malloc(bytes)
• For Example: dynamically allocating a block of memory for an array of
10 int
• int* ptr = (int*) malloc(10 * sizeof(int));

10 x 4 bytes = 40 bytes block of memory


calloc (contiguous allocation)
• dynamically allocate specified number of blocks of memory
• Syntax:
• ptr = (type*)calloc(number of element , size of element);
• For Example: dynamically allocating 10 blocks of memory for an array
of 10 int
• int* ptr = (int*) calloc(10, sizeof(int));
malloc

calloc
realloc (re-allocation)
• dynamically change the size of allocated memory
• in cases of insufficient memory allocation, realloc can dynamically re-
allocate memory.
• Syntax:
• ptr = realloc(ptr, newSize);
• For example : dynamically re-allocate ptr to a larger size
• ptr = realloc(ptr, 20 * sizeof(int));
free
• dynamically de-allocate memory
• dynamically allocated memory needs to be freed after use
• Syntax:
• free(ptr);
Practice 1
There are three strategies to allocate free memory space for processes.
• First fit. Allocate the first hole that is big enough.
• Best fit. Allocate the smallest hole that is big enough.
• Worst fit. Allocate the largest hole.

Create a memory allocation program to show the how the First Fit strategy would
allocate memory and move the spaces in memory.
Given the memory block size is 20 and initially
there are 5 holes with the sizes { 4, 3, 8, 2, 3}
After each allocation, if there are leftover space, the space can be given to the next
hole.
If there are 5 blocks of data with sizes {3,4,3,5,3}. Try to fit them in the block of
memory. How are the hole sizes changed as you allocate and shuffle the memory.
Use a dynamic array to represent hole size and print out the array to show hole size. For example, the
initial hole sizes are { 4, 3, 8, 2, 3}

Pseudocode:
int n[5]={4,3,8,2,3};
int* arr = (int*) calloc(3, sizeof(int));
for (5 holes)
{
realloc(arr size to current hole size);
for(arr size)
print arr[];
}
First fit algorithm
int n[5]={4,3,8,2,3};
bool full[5] = {false, false, false, false, false}; // keep track of hole occupied, true if hole allocated
input data block size;
for(all holes)
{ if((found current hole size >= data size) && not occupied )
{ if (current hole size > data size && next hole not occupied && not last hole)
{ next hole += (current hole size – data size); // give extra space to next hole
}

if (not last && next hole not occupied)


{ current hole size = data size; //allocate
full[current] = true;
}
else
{ full[current] = true;
}
break;
}
}
Sample
output

input sizes
{3,4,3,5,3
}
Test out some
other data
block sizes:
{8,3,2,2,1}
{5,7,1,2,4}
What are the
outputs?
Practice 2
Modify Practice 1, implement the Best Fit strategy instead
For Best Fit,
You need to look through all empty holes and allocate the hole that is
equal to your data size or
the smallest free hole that is large enough for your data (to produce the
least leftover space)

Use the same scenarios and inputs as Practice 1. How different are the
outcomes?

You might also like