Dynamic Array in C

Dynamic arrays in C are resizable arrays that grow or shrink as needed. They are implemented using pointers and memory allocation functions like malloc and realloc.

Creating and Managing a Dynamic Array

  1. Declare a Pointer: Start by declaring a pointer that will point to the dynamic array.
  2. Allocate Memory: Use malloc to allocate an initial amount of memory for the array. This initial size is determined by you.
  3. Check for Allocation Errors: Always check if the memory allocation was successful. malloc may return a NULL pointer if memory allocation fails.
  4. Add or Remove Elements: As you need to add or remove elements, check if the array is full or if you want to resize it using realloc. If so, allocate more memory.
  5. Free Memory: Don't forget to free the allocated memory when you're done with the dynamic array using free.

Dynamic array in C

Dynamic arrays can be created using pointers. The following code shows how to create a dynamic array of integers using pointers

int* my_array = malloc(10 * sizeof(int));

This code allocates a block of memory that is large enough to store 10 integers. The pointer my_array is set to the address of the beginning of the allocated memory block.

The size of the dynamic array can be increased or decreased at runtime using the realloc() function. The following code shows how to increase the size of the dynamic array by 10 elements:

my_array = realloc(my_array, (my_array_size + 10) * sizeof(int));

The realloc() function will allocate a new block of memory that is large enough to store the specified number of elements. The pointer my_array will be updated to point to the new block of memory.

The memory allocated for a dynamic array should be freed when it is no longer needed. The following code shows how to free the memory allocated for the my_array dynamic array:

free(my_array);
Example | C Dymanic Array
#include <stdio.h> #include <stdlib.h> int main() { int *dynamicArray = NULL; // Pointer to the dynamic array int size = 0; // Current size of the dynamic array // Initially allocate memory for 5 integers dynamicArray = (int *)malloc(5 * sizeof(int)); if (dynamicArray == NULL) { perror("Memory allocation failed"); exit(EXIT_FAILURE); } // Add elements to the dynamic array for (int i = 0; i < 10; i++) { if (i >= size) { // If the array is full, resize it using realloc size += 5; dynamicArray = (int *)realloc(dynamicArray, size * sizeof(int)); if (dynamicArray == NULL) { perror("Memory reallocation failed"); exit(EXIT_FAILURE); } } dynamicArray[i] = i * 10; } // Access and print elements for (int i = 0; i < 10; i++) { printf("Element %d: %d\n", i, dynamicArray[i]); } // Free the dynamically allocated memory free(dynamicArray); return 0; }
//Output: Element 0: 0 Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Element 6: 60 Element 7: 70 Element 8: 80 Element 9: 90

In this example, we start with a pointer (dynamicArray) initially allocated for 5 integers and then resize it using realloc whenever it becomes full. This way, we ensure that the dynamic array can accommodate additional elements as needed.

Disadvantages of using dynamic arrays with pointers in C:
  1. Complexity: Dynamic arrays can be more complex to use than static arrays.
  2. Memory management: It is important to manage the memory allocated for dynamic arrays carefully to avoid memory leaks.

Conclusion

Dynamic arrays using pointers in C are a data structure that allows for flexible memory allocation. They utilize pointers to create resizable arrays by dynamically allocating and deallocating memory, enabling efficient storage management for varying data needs in C programming.