0
0
C++programming

new operator in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA pointer to the allocated memory
BThe value stored in the memory
CAn integer representing the size
DNothing, it just allocates silently
Which operator should you use to free memory allocated with new[]?
A<code>delete</code>
B<code>free</code>
C<code>delete[]</code>
D<code>release</code>
What happens if you forget to use delete after new?
AThe program crashes immediately
BThe memory is freed automatically
CThe pointer becomes null
DMemory leak occurs
How do you allocate a single object of a class Car using new?
ACar* c = new Car();
BCar* c = Car();
CCar c = Car();
DCar c = new Car();
Which of the following is true about new?
AIt allocates memory on the stack
BIt allocates memory on the heap
CIt automatically deletes memory
DIt returns a reference
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.