Lecture 5 - Memory Ownership Flashcards

1
Q

Why do we use the C programming language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does C build in a person?

A

Understanding of resource management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a massive drawback of C?

A

The lack of garbage collection, we have to sanitize the program ourselves via malloc and free.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many times can we call free on a certain memory location?

A

Once , otherwise we will get an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why are multiple pointers to the same memory location problematic?

A

As we only need to call free once, so it is very easy to call it a second time when there is multiple pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a good practice when freeing nodes?

A

To set the pointer variable to NULL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a double-free error?

A

Calling free twice on one memory location (has been freed the first time and not malloced).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is calling free with NULL ok?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are dangling pointers?

A

Memory locations not freed, but also have no pointers pointing to them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a memory leak?

A

When free is never called on a malloced piece of memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the most common cause of memory leaks?

A

Reassigning a pointer , thus creating a dangling pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is ownership?

A

A concept related to memory managment. It states that we identify a single entity that is responsible for managing a location in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Tree example of ownership.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Equivalent of printf in C++?

A

std::cout &laquo_space;””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create an auto variable in C++?

A

auto v = ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does C enforce an ownership model?

A

No, that’s why we are learning C++.

17
Q

What is C++?

A

C++ is a superset of C (provides more features and keywords).

18
Q

What is the difference between C and C++?

A

C++ provides OOP, focusing on objects rather than functions. C++ also provides ownership!

19
Q

Do you still use clang to compile C++?

A

We use clang++.

20
Q

What does the -std=x flag specify?

A

The version of the language standard, used with clang and clang++.

21
Q

What is ownership called in C++?

A

RAll = Resource Acquisition is initialization

22
Q

What is std :: cin&raquo_space; in C++?

A

Equivalent to scanf in C.

23
Q

What is sleep() in C++?

A

Pauses execution for some period specified as an argument.

24
Q

What happens in RAll?

A
  • 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
25
Q

What is RAll represented by (in C++.) HINT : OBJECTS

A

struct datatypes and their constructor+destructor

26
Q

What is the constructor?

A

A function called when creating variables

27
Q

What is a destructor?

A

A function called when variable is deleted.

28
Q

CLICK FOR EXAMPLE.

A

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;

29
Q

Does the destructor run automatically?

A

yes, this happens at end of the block

30
Q

What is std :: vector in C++?

A

dynamic array which will store values of the same typ

31
Q

Is this concept of ownership only applicable to memory managment?

A

No, but resource managment in general.

e.g. closing and opening a file

32
Q

Does C++ provide RAll data types?

A

YEs, these will not leak memory and are easy to use.

33
Q

How do we store a single value on the heap?

A

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.

34
Q

How do you make a unique pointer in C++?

A

std :: make_unique<struct>(value_ptr)</struct>