Is there a way in C and C++ to check if a variable has been claimed by garbage collector i.e. check if it has been freed previously.
Is there a way in C and C++ to check if a variable has
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.
There is no garbage collection in C. Although the standard mentions in few places that such implementations are possible. I do not know if any such implementation is existing, though.
In C/C++ there is no way to know if an object is already deleted or not. This is a constant source of problems and errors. Nevertheless we have to live with this.
Primary reason for this design decision – simplicity of the pointer. It should be just an address and nothing else. Otherwise extra overhead is needed. Memory in C++ is used and reused. In place of 2 small objects a while later there can be a big object that will cover the whole space of these 2. This means that there is no way to place something in front of every object to allow checking. Simplicity and speed – these are important goals of C/C++. Problem with freed/not freed/not sure pointers is the price for good performance.