In my C program, I create a child process and in it, it parses a string and created a pointer array (using malloc()
) for the use of passing it in execvp()
command.
But the problem is, how do you free the memory that the child created? execvp()
runs the new task and may or may not return. If it doesn't return, then it was successful and the child dies (and I can't really use the free()
command). If it failed then it returned, then it continues doing the next lines of code, is this the only chance to free the memory?
You don't need to. Specifically, if you allocate memory in a process before an exec()
-type routine (e.g., execvp()
in your case) is called, all of the memory associated with the original executable is released. It's a similar situation to a process exiting (and having all its resources released), and a new process being started with a clean slate.
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