Memory Deallocation
In C, malloc(n) allocates n bytes of memory and returns the address.
int *x = malloc(n);
Free releases the memory immediately. For example, free(x):
de-allocates the memory block beginning at address x
memory block can later be allocated (by later malloc)
does NOT change the value at address pointed to by x
Common Memory Allocation and Access Bugs
Memory leak: Dangling pointer:
What issue does each code block have?
Why even use pointers?
Consider the alternative to passing a pointer to an array or a struct: Pros:
Cons:
Tips for Reference Counting
Three ways of managing dynamic memory allocation
1. Manual ref counting – our way using refcount 2. Automatic ref counting – python, swift
3. Garbage collection – Java
Memory Management Summary
What are the advantages of C’s explicit delete (free)? What are the advantages of reference counting?
What are the advantages of Java’s garbage collection? Is it okay to ignore deallocation in Java programs?