The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment
char *string;
string = (char *)malloc(100);
string = NULL;
One of the most common failure when memory is managed explicitly is what is known as "memory leak". This situation occurs when a program gets dynamic memory, and the pointer value returned by the system is lost by mistake. In this case, it is no longer possible to invoke the free function with that pointer, and the portion of memory is reserved for the remainder of execution.
The first line declares a pointer to character. In the second you reserve a space of 100 bytes. The memory manager returns a pointer to the beginning of that block and stored in the variable string. At that time, the address of that block is not stored anywhere else. The next line assigns the same value to NULL pointer. What has happened with the memory address of the portion you just stay? It has been lost and there is no way to recover it, because string was the only copy of that value. As a result, the portion of memory reserved will marked as busy for the rest of program execution. The memory has escaped.
What happens if the assignment is not successful?
name1 = realloc(name1, 10 * sizeof(char));
then malloc returns NULL, the memory area pointed to by name1 is lost. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.
char* name1 = malloc(4 * sizeof(char));
char* tmp;
if (name1 != NULL)
{
temp = realloc(name1, 10 * sizeof(char));
if (tmp != NULL)
{
name1 = tmp;
}
}