Memory Management Flashcards
(18 cards)
Explain the difference between the stack and the heap
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
You don’t know how big a number is going to be in your program.
How would you create a variable on the heap?
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
We’re finished with our new int operator, free up the space
delete ptr_to_int;
ptr_to_int = nullptr
What is a memory leak?
Make one
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
Create an array of doubles on the heap
double *arrary = new double[4];
array[0] = 1;
etc
delete[] array;
Create a student class,
create a student object on the heap
class Student
{
public:
std::string name;
void Print()
{
std::cout «_space;name «_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();
What is an initializer list
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
}
If your class has a constructor, how do you create memory on the heap for it
Student *student = new Student(“Mary”);
std::cout «_space;student->name;
Create a try catch statement to make sure you have enough memory available or no contiguous block that’s big enough
try
{
double *big_array = new double[99999999999];
delete[] big_array;
}
catch (const std::bad_alloc& exp)
{
std::cout»_space; “Bad_alloc caught: “ «_space;exp.what() «_space;endl;
}
For the catch, bad_alloc is the type and it references exp, exp is also a constant
What are smart pointers?
When you call new, you don’t have to call delete, sometimes you don’t have to even call new either.
What is a unique pointer
When the pointer goes out of scope it will get destroyed.
Can’t be copied
Create a unique pointer that is destroyed after main is finished
include memory
int main()
{
std::unique_ptr<int> ptr = std::make_unique<int>(42);
std::cout << *ptr << std::endl;
return 0;
}</int></int>
What is a shared pointer?
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
Create a shared pointer
class Student
{
public:
std::string name;
Student(std::string n) : name(n) {
std::cout «_space;name «_space;” created.\n”;
}
~Student() {
std::cout «_space;name «_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 «_space;ptr2->name;
std::cout «_space;ptr1.use_count()
return 0;
}
~Student defined the destructor for the class. Whenever an object is deleted this is called.
What is a weak pointer?
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
What does lock() do?
Tries to “lock in” access to the object if it still exists
What are the steps for creating a weak pointer?
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)
Create a weak pointer
include <iostream></iostream>
#include <memory></memory>
class Thing {
public:
void hello() {
std::cout «_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; }