简体   繁体   中英

How to free memory created by malloc after using execvp?

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 (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM