Explicit memory management requires developers to request memory and mark when it may be released. The approach makes allocation and deallocation manual, especially for heap memory, rather than delegating cleanup to garbage collection. section 2.2
Manual control introduces practical trade-offs:
- Requesting too much memory can slow allocation or waste memory. section 2.2
- Requesting too little can cause programs to use memory outside the allocated block. section 2.2
- Stack values are commonly released when their function returns, while heap memory requires greater management attention. 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)
The welcome program places an explicit free operation after the value is no longer needed, making the cleanup point visible. section 2.2
