How to free memory created by malloc after using execvp?
In my C program I create a child process and in it it parses the string and creates an array of pointers (using malloc()
) to use the transfer in the command execvp()
.
But the problem is, how do you free the memory that the child created? execvp()
starts a new task and may or may not return. If it doesn't come back, then it was successful and the child dies (and I can't use the command free()
). If it failed, then it returned, then it continues to do the next lines of code, is this the only chance to free memory?
source to share
You do not need. In particular, if you allocate memory in a process before a procedure of type exec()
-type is called (for example, execvp()
in your case), all memory associated with the original executable will be released. It's a similar situation with a process exiting (and releasing all of its resources) and a new process starts from scratch.
source to share