Results 1 to 3 of 3

Thread: garbage collection: how to handle this situation

  1. #1
    Member
    Join Date
    Jan 2019
    Location
    encode.ru
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    garbage collection: how to handle this situation

    Hello community,
    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:

    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
    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?
    Last edited by unique identifier; 27th January 2019 at 17:11.

  2. #2
    Member
    Join Date
    Jan 2019
    Location
    encode.ru
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I figured it out. Took a moment, but anyway. A solution looks like this:
    Code:
    allocate memory for error_message
    
    if number_of_receivers > 0
        // write the value to the allocated memory
        error_message = "too many receivers"
    
    if number_of_receivers > 10
        // use the variable for the last time
        print error_message
    
    free memory of error_message

  3. #3
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts
    Would be a good idea to mention the language. Is that python or just pseudocode?
    In C/C++ stack is managed automatically based on statement blocks/function nesting,
    so the only way to pass a local variable from one {} block to another is via threads or coroutines,
    which have their own stack.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •