I have a question about variable scope and memory management in C. I am writing a program that listens for a socket connection and then launches a new thread to handle that client. The main while() loop can launch many separate threads. My question is this:
If I don't use dynamic memory allocation [no malloc()], and instead have a variable locally declared as shown below, is it safe to use that variable in the called thread, even after the next iteration of the loop takes place?
while(1)
{
// Accept new socket connection here
// ...
pthread_t pt;
struct mypthreadargs args;
rc = pthread_create(&pt, NULL, handle_client, &args);
// The handle_client() function makes extensive use of the 'args' variable
}
What happens to the args
variable (and the pt
variable, too, for that matter) after the thread has been created. Is the memory lost once the while() loop starts over, or is it safe to use them as I have?
Thanks!