Recall & Review
beginner
What does the
new operator do in C++?The
new operator allocates memory on the heap for a variable or object and returns a pointer to that memory.Click to reveal answer
beginner
How do you use
new to create an integer on the heap?You write
int* p = new int; which allocates memory for an integer and stores its address in pointer p.Click to reveal answer
beginner
What must you do after using
new to avoid memory leaks?You must use
delete to free the memory allocated by new when it is no longer needed.Click to reveal answer
intermediate
How do you allocate an array of 5 integers using
new?Use
int* arr = new int[5]; to allocate an array of 5 integers on the heap.Click to reveal answer
intermediate
What is the difference between
delete and delete[]?delete frees memory allocated for a single object, while delete[] frees memory allocated for an array.Click to reveal answer
What does
new return when it successfully allocates memory?✗ Incorrect
new returns a pointer to the memory it allocates on the heap.Which operator should you use to free memory allocated with
new[]?✗ Incorrect
Memory allocated with
new[] must be freed with delete[] to avoid undefined behavior.What happens if you forget to use
delete after new?✗ Incorrect
Forgetting
delete causes memory leaks because allocated memory is not freed.How do you allocate a single object of a class
Car using new?✗ Incorrect
You allocate with
new and store the pointer: Car* c = new Car();.Which of the following is true about
new?✗ Incorrect
new allocates memory on the heap, not the stack.Explain how the
new operator works and why it is important to use delete afterwards.Think about where memory is allocated and what happens if you don't free it.
You got /4 concepts.
Describe the difference between using
new for a single object and for an array, including how to free each.Focus on syntax and matching delete operators.
You got /4 concepts.