LDP_NotesVU
Dynamic Memory Allocation (DMA) vs Static Memory Allocation (SMA)
Memory Allocation Types
1. Static Memory Allocation (SMA)
Definition: Memory allocation that occurs at compile time.
Characteristics:
Memory size cannot change during program execution.
Faster compared to DMA as all memory allocation is done before the program runs.
Memory is allocated from the stack.
Less efficient for varying data sizes, and we cannot reuse the unused memory.
Use Cases: Typically used in fixed-size arrays and structures where memory requirements are known ahead of time.
2. Dynamic Memory Allocation (DMA)
Definition: Memory allocation that occurs at runtime.
Characteristics:
Memory can be adjusted during execution, allowing for variable-sized arrays and structures.
Allocated memory is generally taken from the heap.
Includes functions like
malloc(),calloc(),realloc(), andfree(). Each function has specific behaviors:malloc(size_t size): Allocates a block of memory of specified size and returns a pointer to it (returns garbage values if uninitialized).calloc(size_t num, size_t size): Allocates memory for an array of elements, initializes them to zero.realloc(void* ptr, size_t new_size): Changes the size of previously allocated memory, making it suitable for dynamic array growth.free(void* ptr): Deallocates previously allocated memory.
Use Cases: Preferred when the size of data is unknown until runtime, such as in linked lists and data structures where data varies widely.
Examples of Allocation Techniques
Example of malloc
int* ptr;
int n;
printf("Enter total number of values: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int)); // Allocates memory for n integersExample of calloc
int* ptr;
int n;
printf("Enter the total number of values: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int)); // Allocates memory for n integers, initialized to zeroExample of using realloc
int* new_ptr;
new_ptr = (int*) realloc(ptr, new_size * sizeof(int)); // Resizes the allocated memoryExample of free
free(ptr); // Frees the allocated memory, preventing memory leaksKey Differences Between SMA and DMA
Allocation Time:
SMA: Occurs at compile time.
DMA: Occurs at runtime.
Memory Resizing:
SMA: Memory size is fixed.
DMA: Memory size can be resized.
Efficiency:
SMA: Generally faster due to less overhead.
DMA: More flexible but can be slower due to time taken to allocate/deallocate memory.
Typecasting in C
Definition: Typecasting is the process of converting one data type to another to perform operations effectively.
Automatic Type Conversion: Sometimes due to expressions involving different data types (for example, int to float).
Syntax Example:
float val_2 = (float) val_1; // Explicitly converting val_1 to floatProgramming Concepts
Example of Type Casting and Dynamic Memory Allocation
int main() {
int total = 13, values = 3;
double average;
average = (double) total / values;
printf("The average is: %f\n", average);
return 0;
}Example of Dynamic Memory Allocation for Arrays
int main() {
int r = 3, c = 4;
int** arr = (int**) malloc(r * sizeof(int*));
for (int i = 0; i < r; i++) {
arr[i] = (int*) malloc(c * sizeof(int));
}
// Insert values into arr and use it
for (int i = 0; i < r; i++) {
free(arr[i]); // Free inner arrays
}
free(arr); // Free outer array
return 0;
}Memory Management Operations
1. Advantages of DMA
Allows for flexible memory management in applications.
Ensures memory usage is optimized, especially in programs needing variable-sized arrays.
/
2. Disadvantages of DMA
More complex to manage compared to SMA, requiring careful deallocation to prevent leaks.
Involves additional overhead that may slow down performance.