It is stated that a correct program should have a one-to-one correspondence between malloc calls and free calls. How does adding a realloc call change the needed number of malloc and free calls? Briefly explain your reasoning.
Answers
Explanation & Answer:
Malloc is a dynamic memory allocator function which is used to allocate memory to a pointing variable by explicitly converting memory segmentation into the desired data type. In the correct implementation of a program after use of the dynamic variable, it's stored value should be free and this does by using free() function. Now comes the need to dynamically change the size of pointing variable assigned earlier by malloc() function, and for that realloc() function is there, which takes the pointing variable and change in size as arguments. In proper implementation after every use of malloc there should be a free call to free the undesired space in the memory, but using realloc can sometimes be more use. realloc permits the usage of previously build malloc variable to new computation and hence it sometimes results in lowering the number of free() used and we just need to use the free() at the very end of the program.