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 = ;
Does C enforce an ownership model?
No, that’s why we are learning C++.
What is C++?
C++ is a superset of C (provides more features and keywords).
What is the difference between C and C++?
C++ provides OOP, focusing on objects rather than functions. C++ also provides ownership!
Do you still use clang to compile C++?
We use clang++.
What does the -std=x flag specify?
The version of the language standard, used with clang and clang++.
What is ownership called in C++?
RAll = Resource Acquisition is initialization
What is std :: cin»_space; in C++?
Equivalent to scanf in C.
What is sleep() in C++?
Pauses execution for some period specified as an argument.
What happens in RAll?
- We tie the management of a resource to the lifetime of a variable on the stack
- The allocation of the resource (e.g. the malloc call) is done when we create the
variable - The deallocation of the resource (e.g. the free call) is done when the variable is
destroyed
What is RAll represented by (in C++.) HINT : OBJECTS
struct datatypes and their constructor+destructor
What is the constructor?
A function called when creating variables
What is a destructor?
A function called when variable is deleted.
CLICK FOR EXAMPLE.
struct ints_on_the_heap {
int * ptr;
ints_on_the_heap(int size) {
ptr = (int*)malloc(sizeof(int) * size);
}
~ints_on_the_heap() { free(ptr); }
};
typedef struct ints_on_the_heap ints_on_the_heap;
Does the destructor run automatically?
yes, this happens at end of the block
What is std :: vector in C++?
dynamic array which will store values of the same typ
Is this concept of ownership only applicable to memory managment?
No, but resource managment in general.
e.g. closing and opening a file
Does C++ provide RAll data types?
YEs, these will not leak memory and are easy to use.
How do we store a single value on the heap?
using one of the smart pointers:
- std::unique_ptr for unique ownership
the default case, when a single variable owns the value on the heap
(if parent variable goes then so do the children) - std::share_ptr for shared ownership
when it is not possible to identify a single owner (e.g. multi-threaded progs)
We alternatively use plain pointers when we have a non-owning relationship.
How do you make a unique pointer in C++?
std :: make_unique<struct>(value_ptr)</struct>