Knowlet

Unit 12: Dynamic Memory Management

1. Introduction to Dynamic Memory Management

Dynamic Memory Management (DMM) is a method in C programming that allows a programmer to allocate or deallocate memory during the execution (runtime) of a program. Unlike static allocation (where memory is fixed at compile time, such as in standard arrays), DMM provides flexibility in handling data whose size may change during program execution.

Key Difference: Static memory is allocated on the Stack, while dynamic memory is allocated on the Heap.

2. Memory Management Functions: malloc, calloc, realloc, free

C provides four standard library functions for dynamic memory management, defined in the <stdlib.h> header file.

1. malloc() - Memory Allocation

The malloc() function allocates a single large block of contiguous memory of a specified size. It returns a void pointer which can be cast into any type.

ptr = (cast_type*) malloc(byte_size);
  • Initial Value: Allocated memory contains garbage values.
  • On Failure: Returns NULL.

2. calloc() - Contiguous Allocation

The calloc() function allocates multiple blocks of memory, each of the same size.

ptr = (cast_type*) calloc(n, element_size);
  • Initial Value: Automatically initializes all bytes to zero.
  • On Failure: Returns NULL.

3. realloc() - Re-allocation

The realloc() function is used to change the size of previously allocated memory. It is useful when the initial allocation is insufficient or too large.

ptr = (cast_type*) realloc(ptr, new_byte_size);

4. free() - De-allocation

The free() function is used to manually release the memory allocated by malloc() or calloc(). If memory is not freed, it leads to a Memory Leak.

free(ptr);

3. Pointers and Arrays

There is a very close relationship between pointers and arrays in C. An array name itself acts as a constant pointer to the first element of the array.

Key Concepts:

  • Base Address: The name of an array (e.g., arr) represents the address of its first element (&arr[0]).
  • Accessing via Pointers: The element arr[i] can also be accessed using pointer notation as *(arr + i).
  • Pointer Increment: If ptr = arr, then ptr++ moves to the next element in the array based on the data type's size.

4. Pointers and Functions

Pointers can be passed to functions as arguments, allowing the function to modify the original variable in memory. This is known as Call by Reference.

Passing Pointers to Functions:

 void update(int *p) { *p = *p + 10; // Modifies the value at the address stored in p } 

Returning Pointers from Functions:

Functions can also return pointers. However, you should never return the address of a local variable because it is destroyed when the function finishes. Always return addresses of static variables or dynamically allocated heap memory.

5. Exam Focus Enhancements

Exam Tips
  • malloc vs calloc: This is a frequent comparison question. Remember: malloc takes 1 argument and leaves garbage; calloc takes 2 arguments and initializes memory to zero.
  • Type Casting: Always cast the pointer returned by malloc/calloc (e.g., (int*)malloc...) to avoid compiler warnings and ensure compatibility.
  • Check for NULL: In exams, always show a check for NULL after allocation to demonstrate safe programming practices.
Common Mistakes
  • Dangling Pointer: Accessing a pointer after the memory it points to has been free()'d. Always set the pointer to NULL after freeing.
  • Memory Leak: Forgetting to call free(). In long-running programs, this can consume all available RAM.
  • Invalid Pointer Arithmetic: Trying to increment a constant array name (e.g., arr++ is illegal; ptr++ is legal).
Frequently Asked Questions

Q: What happens if realloc() cannot find a contiguous block of the new size?
A: It will allocate a new block elsewhere, copy the existing data, free the old block, and return the new address.

Q: Why is dynamic memory allocation needed?
A: It allows us to handle data structures like Linked Lists and Trees where the size isn't known until the program runs.

Q: Can free() be used on a static array?
A: No. free() can only be used on pointers that were allocated using the dynamic allocation functions (malloc, calloc, realloc).

Did this resource help you study?

Share feedback or report issues to help improve this resource.