Is it possible to adapt unique_ptr for plain c?
Perhaps if there is a way of simulating calls to a home made “constructor/destructor” when calling malloc/free?
Is it doable? Or is it just a silly idea?
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.
The whole point of a “smart pointer” is to do certain tasks automatically on destruction. Since C doesn’t have destructors there’s no way to accomplish this except with explicit function calls – but that’s how you deallocate memory in C already.
You could possibly make a list of pointers that need to be freed and do them all at the same time with a single function call.