if this is a bad idea, how to allocate memory in the function?
Share
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.
It’s not a “bad idea”, but rather “sometimes a bad idea”, which can be said about many ideas in programming.
Allocating memory inside a function and releasing it outside may be a common design pattern, by the way. Consider:
htwas allocated in a functionhashtable_newand released outside it, yet you will see this pattern over and over in lots of good C code.What it does show, however, is how the same logical unit (the hash-table ADT) takes care of allocation and de-allocation. This makes lots of sense – because the one who knows how to allocate, knows best how to deallocate. Allocating and releasing in different logical units is more often a bad idea.