Lecture 5 - Memory Ownership Flashcards
Why do we use the C programming language?
It is powerful, fast and very versatile (used in games , kernels, compilers, networking etc.)
It is also quite lightweight and can be used in constrained systems.
What does C build in a person?
Understanding of resource management.
What is a massive drawback of C?
The lack of garbage collection, we have to sanitize the program ourselves via malloc and free.
How many times can we call free on a certain memory location?
Once , otherwise we will get an error
Why are multiple pointers to the same memory location problematic?
As we only need to call free once, so it is very easy to call it a second time when there is multiple pointers.
What is a good practice when freeing nodes?
To set the pointer variable to NULL.
What is a double-free error?
Calling free twice on one memory location (has been freed the first time and not malloced).
Is calling free with NULL ok?
Yes
What are dangling pointers?
Memory locations not freed, but also have no pointers pointing to them.
What is a memory leak?
When free is never called on a malloced piece of memory.
What is the most common cause of memory leaks?
Reassigning a pointer , thus creating a dangling pointer.
What is ownership?
A concept related to memory managment. It states that we identify a single entity that is responsible for managing a location in memory.
Tree example of ownership.
we might say that the parent in a tree owns its children
i.e. it is responsible for allocating and freeing the memory used by its children
Equivalent of printf in C++?
std::cout «_space;””
How do you create an auto variable in C++?
auto v = ;