I think what I'm doing is safe but I want to check with all you smart people first.
I know returning a pointer to a local variable from a function is bad!
But what if a pointer is allocated outside of a function call and passed in as input. Then inside the function call a local pointer is made, initialized to the input pointer, used to set some struct->members, and then returned?
Here's an example using (int *) instead of my code's (struct type_s *):
int *modify_p_x(int *p_x)
{
int *new_p_x = p_x;
*new_p_x = 100;
return new_p_x;
}
int main(int argc, char **argv)
{
int x = 42;
int *p_x = &x;
p_x = modify_p_x(p_x);
return 0;
}
Since new_p_x is initialized using p_x and p_x has a lifetime irrespective of themodify_p_x function call, does that make this code safe?
Thanks.