Memory Management Flashcards

(18 cards)

1
Q

Explain the difference between the stack and the heap

A

Stack - when we know how much data needs to be stored at compile time

Heap - This is dynamically created during the programs run. Different space in memory

If we have a game where we don’t know how many enemy characters will be needed, this will need to go on the heap

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

You don’t know how big a number is going to be in your program.
How would you create a variable on the heap?

A

int *ptr_to_int = new int;

This creates a pointer on the stack that points to the heap

  • = dereferenc operator

*prt_to_int = 5; <- 5 is stored in the memory address on the heap

or just

int *ptr_to_int = new int(5)

after you delete make sure to:
ptr = nullptr

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

We’re finished with our new int operator, free up the space

A

delete ptr_to_int;
ptr_to_int = nullptr

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

What is a memory leak?

Make one

A

int *ptr_to_int = new int(5);

prt_to_int = new int(10);

delete ptr_to_int;

ptr_to_int points to the heap with a value of 5

Next, it points to a different memory location on the heap with 10

delete only deletes the new memory location, the previous is now there forever

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

Create an array of doubles on the heap

A

double *arrary = new double[4];

array[0] = 1;
etc

delete[] array;

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

Create a student class,
create a student object on the heap

A

class Student
{
public:
std::string name;
void Print()
{
std::cout &laquo_space;name &laquo_space;endl;
}
}

Student student = new Student
(
student).name = “John”
(*student).print();

we put *student in () because we want to make sure this runs first before the .

or

student->name = “Mary”;
student->print();

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

What is an initializer list

A

When you create a constructor and ask for a variable to be put in so it can update the main variable of the class it takes two steps, and name would be an empty string for a bit.

Initializer lists automatically plug that value in for you without you having to specify it

class Student{
std::string name;
public:
Strudent(std::string n) : name(n) {};

When the program makes ‘name’ here it immediately uses n right away.

Look further into this
}

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

If your class has a constructor, how do you create memory on the heap for it

A

Student *student = new Student(“Mary”);

std::cout &laquo_space;student->name;

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

Create a try catch statement to make sure you have enough memory available or no contiguous block that’s big enough

A

try
{
double *big_array = new double[99999999999];
delete[] big_array;
}
catch (const std::bad_alloc& exp)
{
std::cout&raquo_space; “Bad_alloc caught: “ &laquo_space;exp.what() &laquo_space;endl;
}

For the catch, bad_alloc is the type and it references exp, exp is also a constant

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

What are smart pointers?

A

When you call new, you don’t have to call delete, sometimes you don’t have to even call new either.

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

What is a unique pointer

A

When the pointer goes out of scope it will get destroyed.

Can’t be copied

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

Create a unique pointer that is destroyed after main is finished

A

include memory

int main()
{
std::unique_ptr<int> ptr = std::make_unique<int>(42);
std::cout << *ptr << std::endl;
return 0;
}</int></int>

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

What is a shared pointer?

A

smart pointer that can be copied
uses REFERENCE COUNTING
It counts down until the last reference is done before destroying itself.

Deleted when both are out of scope

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

Create a shared pointer

A

class Student
{
public:
std::string name;
Student(std::string n) : name(n) {
std::cout &laquo_space;name &laquo_space;” created.\n”;
}
~Student() {
std::cout &laquo_space;name &laquo_space;” destroyed.\n”;
}
};

int main()
{
std::shared_prt<Student> ptr1 = std::make_shared<Student>("Alice");
std::share_ptr<Student> ptr2 = ptr1;</Student></Student></Student>

std::cout &laquo_space;ptr2->name;
std::cout &laquo_space;ptr1.use_count()
return 0;
}

~Student defined the destructor for the class. Whenever an object is deleted this is called.

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

What is a weak pointer?

A

Refers to an object managed by shared pointer

Does not affect the reference count of the object

A shared pointer is someone holding a balloon, it stays there as long as someone’s holding it

A weak pointer just watches the balloon but doesn’t hold it up

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

What does lock() do?

A

Tries to “lock in” access to the object if it still exists

17
Q

What are the steps for creating a weak pointer?

A

Create a weak pointer with no value

Create a shared pointer

Give weak pointer the value of the shared pointer

Create a temp shared pointer that tries to lock-in the value of the weak pointer if it exists (if it exists it gives you the value, if not it gives you a nullptr)

18
Q

Create a weak pointer

A

include <iostream></iostream>

#include <memory></memory>

class Thing {
public:
void hello() {
std::cout &laquo_space;“Hello from Thing!\n”;
}
};

int main() {
std::weak_ptr<Thing> weak;</Thing>

{
    // Create a shared_ptr and a Thing object
    std::shared_ptr<Thing> shared = std::make_shared<Thing>();
    weak = shared;  // weak points to the same Thing

    // Lock the weak_ptr to get a usable shared_ptr
    std::shared_ptr<Thing> temp = weak.lock();

    if (temp) {
        temp->hello();  // Call the hello() function
    }
} // shared_ptr is destroyed here, object is deleted

// Try to use weak_ptr again
std::shared_ptr<Thing> temp = weak.lock();

if (temp) {
    temp->hello();
} else {
    std::cout << "The object no longer exists.\n";
}

return 0; }