1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Where are fixed-size arrays stored?
Stack memory
How is stack memory managed?
Automatically managed by the program
What is a limitation of fixed-size arrays?
Size must be known at compile time and cannot change
Why is dynamic memory allocation needed?
When the required size is only known at runtime.
What is the heap?
A region of memory managed by the programmer
What operator allocates memory on the heap?
new
int* p = new int;How do you allocate memory for a single variable?
int* p = new int;How do you allocate memory for an array?
int* arr = new int[n];How do you release dynamically allocated memory?
delete p;
delete[] arr;Why is delete[] used instead of delete for arrays?
To release memory allocated for an array
What does the constructor do in the IntArray example?
#include <iostream>
class IntArray
{
private:
int *data_;
int size_;
public:
IntArray(int s)
{
size_ = s;
data_ = new int[size_];
for (int i = 0; i < size_; ++i)
{
data_[i] = 0;
}
}Allocates memory and initializes all elements to 0
What does the copy constructor do?
Creates a new array and copies every element
What does the assignment operator do?
Deletes old memory, allocates new memory, and copies the data
Why does the assignment operator check this != &other?
To avoid self-assignment
What does the destructor do?
Releases dynamically allocated memory using delete[].
Why do a and b remain independent after copying?
Each object has its own dynamically allocated array
What is a memory leak?
Dynamically allocated memory is not released after it is no longer needed
What causes a memory leak?
Using new without calling delete
What can happen if memory leaks accumulate?
The program can slow down or crash
How can memory leaks be avoided?
Ensure every new has a corresponding delete
Why can exceptions cause memory leaks?
delete may never be reached
How does modern C++ help avoid memory leaks?
By using smart pointers
What is a smart pointer?
An object that behaves like a pointer but automatically manages dynamic memory
What is one benefit and one cost of smart pointers?
Prevent memory leaks; slightly more overhead
When should std::unique_ptr be used?
When an object should have exactly one owner
std::unique_ptr<int[]> p(new int[size]);What happens when a std::unique_ptr goes out of scope?
The object is automatically destroyed
Can a std::unique_ptr be copied?
No, ownership must be transferred using move()
std::unique_ptr<int[]> p1(new int[5]);
std::unique_ptr<int[]> p2;
p2 = std::move(p1);How is ownership transferred with std::unique_ptr?
p2 = std::move(p1);When should std::shared_ptr be used?
When multiple parts of a program must own the same object
std::shared_ptr<int[]> p(new int[size]);What is a circular reference?
Two or more objects reference each other, preventing the reference count from reaching zero
When should std::weak_ptr be used?
To observe a shared object without owning it and avoid circular references
std::shared_ptr<int[]> p(new int[size]);
std::weak_ptr<int[]> w = p;