GROUP 1 REG NUMBER
MUKISA EMMANUEL 24/1/306/D/265
NAKIBUUKA KIMBERLY DREDAH 23/1/370/D/024
AYEBARE CYNTHIA 24/1/306/DJ/873
KATO JUSPER NULL
GROUP 2
PENGERE DAVID ISRAEL 24/1/306/D/
GAKURU PAUL 23/1/370/D/128
OCHOLA PETER 24/1/306/682
NAKUBULWA WINFRED
MEMORY ALLOCATION
Understanding Static and Dynamic Memory
Allocation
Definition
• Refers to the process of assigning memory space to
programs or processes.
• Critical in systems programming for efficient resource
management.
Types of Memory Allocation
• Static memory allocation
• Dynamic memory allocation
Static Memory Allocation
• The memory requirements are known at compile time.
• Specifically,after a program compiles, the compiler can
perfectly predict how much memory will be needed and
when for statically allocated variables.
• Static memory is stored in the stack.
• Example:
int arr[10]; // statically allocated array
Dynamic Memory Allocation
• Memory is allocated during runtime.
• Dynamic memory allocation stored in heap.
• Memory size can change dynamically as the program
runs.
• – the input may affect memory allocation.
• NB: If you want to allocate memory in one function, and
have that memory available after the function is
completed, you have to allocate memory dynamically in
that function
Dynamic Memory Management Functions
• Four memory management functions are used with
dynamic memory in the C language.
• malloc(), calloc(), and realloc() are used for memory
allocation.
• free() is used to return allocated memory to the system
when it is no longer neededAll the memory
management functions are found in the standard library
header file <stdlib.h>.
malloc()
Formal Description:
• It allocates unused space for an object whose size in
bytes is specified by size and whose value is unspecified,
and returns a pointer to the beginning of the memory
allocated.
• If the memory can’t be found, NULL is returned.
void *malloc(size_t size);
CODE
• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• int *A;
• A = (int *)malloc(sizeof(int));
• *A = 5;
• printf(“A is %d”,*A);
• return 0;
• }
• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• int *A, *B; // declare 2 pointers
• // allocate memory for the pointers
• A = (int *)malloc(sizeof(int));
• B = (int *)malloc(sizeof(int));
• *A = 5; // Store 5 where A is pointing to
• *B = 17; // Store 17 where B is pointing to
• printf(“B = 0x%x”, B);
• } printf(“*B = %d, *B);
• Suppose you try to print the value of the pointer stored in B?
The printf statement prints an answer such as B == 0xf6da, (this is hexadecimal since %x prints values in
hex.)
C permits pointer values to be printed.
• B is the memory address of the int stored there.
calloc()
• Allocates memory for an array of elements and initializes
all bits to zero.
• Returns a pointer to the beginning of the memory
allocated.
• If the memory can’t be found, NULL is returned
• void *calloc(size_t nelem, size_t elsize);
Using calloc instead of malloc
We used: p = (int *)malloc(size*sizeof(int));
We could have done :
p = (int *)calloc(size, sizeof(int));
But for this example there was no need to initialize the whole block of memory to 0
Which is the benefit of calloc
So when you want to initialize all the memory locations to 0
calloc is the right choice since it does it for you
calloc code
• #include <stdio.h>
• #include<stdlib.h>
• int main() {
• int*ptr, size=5;
• ptr=(int*)calloc(size,sizeof(int));
• for(int i=0; i<size;i++) {
• ptr[i]=i+1;
• printf(“%d”, ptr[i]);
• }
• free(ptr)
• retuirn 0;
• }
Difference between calloc and malloc
• What’s the difference?
• Both descriptions basically say that you need to tell the
• function how many bytes to allocated
• – How you specify this to the two functions is different
free()
• Frees dynamically allocated memory.
• Example:
free(ptr);
free() code example
• #include <stdio.h>
• #include<stdlib.h>
• int main() {
• int*ptr, size=5;
• ptr=(int*)malloc(size*sizeof(int));
• for(int i=0; i<size;i++) {
• ptr[i]=i+1;
• printf(“%d”, ptr[i]);
• }
• free(ptr)
• return 0;
• }
realloc()
• Resizes previously allocated memory.
• Sometimes an array gets filled but you want to “extend” it because more
elements must be stored.
• Based on dynamic memory allocation this could be solved by:
1) Allocating new memory larger than the old memory.
2) Copying over all the values from the old memory to the new.
3) Freeing the old memory.
4) Now we can add new values to the new memory.
• But we can avoid all this extra work by using the realloc function that does
it for us:
void *realloc(void *ptr, size_t size);
realloc code
• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• int num_elements = 5;
• Int *ptr = NULL;
• ptr = (int*) malloc(num_elements * sizeof(int));
• if (ptr == NULL) {
• printf(“Memory allocation failed!\n”);
• return 1;
• }
• for (int i = 0; i < num_elements; i++) {
• ptr[i] = (i + 1) * 10; // Assign values: 10, 20, 30, 40, 50
• }
• printf(“Values in allocated memory before reallocation:\n”);
• for (int i = 0; i < num_elements; i++) {
• printf(“ptr[%d] = %d\n”, i, ptr[i]);
• }
•
Code Continuation
• int new_size = 8; // New size (number of elements)
• ptr = (int*) realloc(ptr, new_size * sizeof(int));
• if (ptr == NULL) {
• printf(“Memory reallocation failed!\n”);
• return 1; // Exit the program if memory reallocation fails
• }
• for (int i = num_elements; i < new_size; i++) {
• ptr[i] = (i + 1) * 10; // Assign values: 60, 70, 80
• }
• printf(“\nValues in allocated memory after reallocation:\n”);
• for (int i = 0; i < new_size; i++) {
• printf(“ptr[%d] = %d\n”, i, ptr[i]);
• }
• free(ptr);
• return 0;
• }
Pitfalls:
• A pitfall is a hidden or unexpected problem, danger, or difficulty that can cause trouble or failure.
• In the context of programming, pitfalls refer to common mistakes, errors, or issues that can lead to
bugs, crashes, or other undesirable outcomes.
Common pitfalls
• 1. Memory leaks: Failing to free allocated memory.
• 2. Dangling pointers: Accessing memory after it's been freed.
• 3. Buffer overflows: Writing beyond allocated memory bounds.
Example where memory allocation can be used.
• 1. Dynamic arrays: Allocate memory for arrays based on user input.
• 2. Linked lists: Allocate memory for nodes and links.
• 3. Trees and graphs: Allocate memory for nodes and edges.