Assuming that I have allocated memory using malloc(), if I do in my code:
char *newline = realloc ( oldline , newsize );
// Assuming oldline is the pointer to which the starting address
// of the memory which malloc() has returned, is assigned and,
// say, newsize is integer having 100 value.
Now my questions regarding it are:-
newline will point to same address as oldline, which holds the previous initial address? oldline will be freed(implicitly) and newline will take the charge of memory addressing? After the above code and after the work has been done, what should I do to free memory
free(newline);
or
free(oldline);
or both?
It depend if realloc was successful or not. If realloc is successful then:
No ! For example, if there is not enough contiguous memory after oldline then newline will be different from oldline.
Yes
free(newline); since oldline has been freed if necessary. After a realloc oldline should be considered as invalid pointer.
If it is not successful. Then, you use oldline as if nothing happened, in particular you should free it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With