Malloc, Calloc, Free, Realloc
In the C programming language, the size of an array cannot be changed in the same way as it is changed in Python. In Python, list manipulation commands like slicing and appending easily allow you to resize arrays until they have an appropriate size. However, in C, this task is less straightforward - you have to use the stdlib.h library. The relevant functions in the stdlib.h library that allow you to resize the storage space for your data are malloc, calloc, free, and realloc. Firstly, I'm going to briefly describe these four functions in terms of their purposes, functionalities, and working mechanisms - after all, it is important to know the weaknesses of a function before embarking on the task of optimizing the function.
1) Malloc (Memory Allocation): Malloc is a function in C that dynamically allocates a large contiguous block of memory with a size that is specified by the user when they call the function. Malloc first calls a low-level system memory management function called sbrk() which finds available space in the system's heap that it can allocate to the usr. Sbrk() then returns a pointer (of type
ptr = (void
*) malloc
(size_t
size);
2) Calloc (Contiguous Allocation): Calloc is a function in C that is used to dynamically allocate a user-specified number of blocks of memory of a specific type which it returns to the user. It is functionally similar to malloc() since it also uses the sbrk() function, but differs because Calloc initializes each block with a default value of 0, and because it has two parameters whereas Malloc only has one parameter.
ptr = (void
*) calloc
(size_t
number_of_blocks, size_t
size_of_each_block);
3) Free (Deallocation): Free is a function in C that is used to dynamically deallocate memory to the heap that has been previously allocated to the user. This function is necessary since memory from malloc() and calloc() do not deallocate themselves after the user has finished using them. Freeing memory after using it is an imperative coding practice since it helps mitigate memory wastage. Once the memory space is freed, the pointer can never be used again: it would be pointing to restricted space in the system which would lead to an error.
ptr = void
free
(void
*ptr);
4) Realloc (Reallocation): Realloc is a function in C that is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated to the user (through malloc or calloc) is too small or too large for the user's wishes, realloc can be used to dynamically reallocate memory to the user. Realloc works by allocating (through malloc) the memory space with the size specified by the user, copying the data from the old memory space to the new memory space, freeing the pointer associated to the old memory space, and finally returning the new pointer to the user. The reallocation of memory retains as much of the existing data as possible. Specifically, if the storage space is increased from n bytes to m bytes, the last m-n new bytes will be initialized with default garbage values. Similarly, if the storage size is decreased to n bytes, the first n bytes of the initial memory space will be copied to the new memory space.
ptr = (void
*) realloc
(void
*ptr, size_t
size);