Memory deallocation releases memory when stored data is no longer needed, allowing the computer to reuse that space. Rust calls this cleanup dropping a value. section 2.2
Languages commonly handle deallocation in two ways:
- Manual memory management requires developers to request memory and mark when it can be cleaned up. section 2.2
- Garbage collection, or automated memory management, uses background code to find allocated blocks no variables reference and deallocate them. section 2.2
- Manual-management languages commonly clean stack values when a function returns, while heap memory requires greater care. section 2.2
A welcome program
A simple program allocates memory for a user's name, prints a welcome message, and then explicitly calls free(name). Moving that call inside the welcome function changes where cleanup occurs while preserving the basic task. section 2.2
Listing 2.6 The welcome program written with K
def welcome(name):
print('Welcome ' + name)
name = input('Please enter your name: ')
welcome(name)
free(name)
Shows explicit deallocation in a small welcome program, making the cleanup point visible beside allocation and use. section 2.2
