I would like to implement automatic garbage collection in a compiler for variables which are stored on the stack. If a variable is not thrown from the stack then it's blocking other variables from being deleted, too, because I do not move variables around. I only pop from the stack/delete the top most content on the stack.
I have a difficult situation and I am unsure how to handle the garbage collection: The definition of the variable and the last usage are both in separate conditional code segments. Example:
Does anyone know how to handle this situation? When should I delete the variable (in the example "error_message"). Or should I delete it at all?Code:if number_of_receivers > 0 // define a variable error_message = "too many receivers" if number_of_receivers > 10 // use the variable for the last time print error_message