C language content

Dynamic memory allocation in C is a powerful feature that allows you to allocate memory at runtime. This is particularly useful for creating data structures whose size may not be known until the program is running. The C standard library provides several functions to handle dynamic memory allocation: ‘malloc‘, ‘calloc‘, ‘realloc‘, and free.

'malloc'

Function Prototype:

				
					void* malloc(size_t size);

				
			

Description:

  • malloc stands for “memory allocation.”
  • It allocates a block of memory of the specified size (in bytes) and returns a pointer to the beginning of the block.
  • The contents of the allocated memory are not initialized; they contain garbage values.

Example:

				
					int *ptr = (int*) malloc(10 * sizeof(int));
if (ptr == NULL) {
    // Handle memory allocation failure
}

				
			

'calloc'

Function Prototype:

				
					void* calloc(size_t num, size_t size);

				
			

Description:

  • calloc stands for “contiguous allocation.”
  • It allocates memory for an array of num elements, each of a specified size (in bytes).
  • The allocated memory is initialized to zero.

Example:

				
					int *ptr = (int*) calloc(10, sizeof(int));
if (ptr == NULL) {
    // Handle memory allocation failure
}

				
			

In this example, calloc allocates memory for an array of 10 integers and initializes all elements to zero.

'realloc'

Function Prototype:

				
					void* calvoid* realloc(void* ptr, size_t size);
loc(size_t num, size_t size);

				
			

Description:

  • realloc stands for “reallocation.”
  • It resizes a previously allocated memory block pointed to by ptr to the new size (in bytes).
  • The contents of the memory block are unchanged up to the minimum of the old and new sizes.
  • If the new size is larger, the newly allocated memory will contain uninitialized values.
  • If ptr is NULL, realloc behaves like malloc.
  • If size is zero, realloc behaves like free.

Example:

				
					int *ptr = (int*) malloc(10 * sizeof(int));
if (ptr == NULL) {
    // Handle memory allocation failure
}

// Resize the allocated memory
ptr = (int*) realloc(ptr, 20 * sizeof(int));
if (ptr == NULL) {
    // Handle memory allocation failure
}

				
			

In this example, realloc resizes the memory block initially allocated for 10 integers to now hold 20 integers.

'free'

Function Prototype:

				
					void free(void* ptr);

				
			

Description:

  • free deallocates the memory previously allocated by malloc, calloc, or realloc.
  • The memory block pointed to by ptr is freed, making it available for future allocations.
  • If ptr is NULL, no operation is performed.

Example:

				
					int *ptr = (int*) malloc(10 * sizeof(int));
if (ptr == NULL) {
    // Handle memory allocation failure
}

// Use the allocated memory

// Deallocate the memory
free(ptr);
ptr = NULL; // Optional: set the pointer to NULL to avoid dangling pointer

				
			

In this example, realloc resizes the memory block initially allocated for 10 integers to now hold 20 integers.

Key Points

  1. Memory Allocation Failures: Always check if the memory allocation functions return NULL, which indicates a failure to allocate the requested memory.
  2. Memory Leaks: Failing to free dynamically allocated memory using free results in memory leaks, which can exhaust the available memory over time.
  3. Initialization: Unlike calloc, malloc and realloc do not initialize the allocated memory. You may need to manually initialize it if necessary.
  4. Dangling Pointers: After calling free, setting the pointer to NULL can prevent accidental usage of the freed memory, which could lead to undefined behavior.

By using these functions appropriately, you can manage memory dynamically in C, allowing for more flexible and efficient programs.

Scroll to Top