Is this okay to do in c?
int *i;
// do stuff
i = NULL;
i = (int *) some_func();
// do stuff
if (i != NULL)
free(i);
i = NULL;
// do stuff
i = (int *) some_func();
// do stuff
if (i != NULL)
free(i);
i = NULL;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1) That depends on the contract you have with some_func(). If some_func expects you to call free() on its return value, your code is ok.
2) It’s ok, though not terribly elegant, to reuse variables in C. It’s generally better to use different variables for different purposes. From a performance and mem-usage perspective it completely the same thing.