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

1. Distinguish between static and dynamic memory allocation.

With suitable example explain the memory management


functions.

In computer programming, memory allocation refers to the process


of assigning memory locations to variables, data structures, and
other program components. Memory allocation can be done in two
ways: static memory allocation and dynamic memory allocation.
 Static memory allocation refers to the allocation of memory at
compile time,
 the memory is reserved for the entire execution of the
program.
 In static memory allocation, the memory is allocated to the
program components such as variables and arrays before the
program execution begins.
 Static memory allocation is useful for data that is known at
compile time and does not change during program execution.
 For example, the following code allocates a static array of size
10 at compile time:
int main() {
int num = 42; // A static variable that is allocated at compile-time
printf("The value of num is: %d\n", num);
return 0;
}
Dynamic memory allocation, on the other hand, refers to the
allocation of memory at runtime.
 In dynamic memory allocation, memory is allocated and
deallocated as required during program execution.
 Dynamic memory allocation is useful for data structures whose
size is not known at compile time or whose size can change
during program execution.
For example, the following code allocates a dynamic array of size n at
runtime using the malloc() function:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, sum = 0;
int* arr; // A pointer to hold dynamically allocated memory

printf("Enter the number of elements: ");


scanf("%d", &n);

arr = (int*) malloc(n * sizeof(int)); // Dynamically allocate memory

if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

printf("Enter the elements of the array:\n");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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


sum += arr[i];
}

printf("The sum of the array elements is: %d\n", sum);

free(arr); // Deallocate memory

return 0;

}Memory management functions are used to allocate and deallocate


memory during program execution. Some of the commonly used
memory management functions in C programming language are:
malloc(size_t size): This function is used to allocate memory of the
specified size in bytes. The function returns a pointer to the allocated
memory block.
calloc(size_t n, size_t size): This function is used to allocate memory
for an array of n elements, each of size bytes. The function initializes
the allocated memory to zero.
realloc(void* ptr, size_t size): This function is used to resize the
previously allocated memory block pointed to by ptr to the specified
size in bytes. If the size is smaller than the original size, the function
may truncate the memory block. If the size is larger than the original
size, the function may allocate a new memory block and copy the
contents of the old block to the new block.
free(void* ptr): This function is used to deallocate the previously
allocated memory block pointed to by ptr. Once the memory is
deallocated, it can no longer be accessed by the program.

You might also like