What if your program could grow and shrink its memory exactly when needed, without wasting a single byte?
Why new operator in C++? - Purpose & Use Cases
Imagine you want to create a program that needs to store a list of user names, but you don't know how many users there will be when you write the code.
You try to create a fixed-size array to hold the names, but sometimes it's too small, and other times it wastes a lot of memory.
Using fixed-size arrays means you either run out of space or waste memory.
Changing the size later means rewriting code and copying data manually, which is slow and error-prone.
This makes your program less flexible and harder to maintain.
The new operator lets you ask the computer to give you exactly the amount of memory you need while the program is running.
This means you can create arrays or objects that fit your data perfectly, even if you don't know the size beforehand.
int users[10]; // fixed size, may be too small or waste memory
int* users = new int[userCount]; // size decided at runtime
You can build flexible programs that adapt to changing data sizes without wasting memory or crashing.
Think of a photo gallery app that loads images based on how many photos a user has. Using new, it can create just enough space to hold all photos, no matter the number.
Fixed-size arrays limit flexibility and waste memory.
new lets you allocate memory dynamically during program execution.
This makes programs more adaptable and efficient.
Practice
new operator do in C++?Solution
Step 1: Understand the purpose of
Thenewnewoperator is used to allocate memory on the heap during program execution.Step 2: Identify what
It returns a pointer to the allocated memory so the program can use it.newreturnsFinal Answer:
Allocates memory dynamically and returns a pointer to it -> Option AQuick Check:
new= allocate memory [OK]
- Confusing new with delete
- Thinking new declares stack variables
- Assuming new copies data
new?Solution
Step 1: Check pointer declaration
Usingnewreturns a pointer, so the variable must be a pointer type likeint *.Step 2: Verify syntax correctness
int *p = new int;correctly allocates an int and assigns its address top.Final Answer:
int *p = new int; -> Option DQuick Check:
Pointer = new type [OK]
- Declaring variable without pointer
- Using parentheses incorrectly
- Assigning new to non-pointer variable
int *arr = new int[3]{10, 20, 30};
std::cout << arr[1] << std::endl;
delete[] arr;Solution
Step 1: Understand array allocation with new
The code creates an array of 3 integers with values 10, 20, 30.Step 2: Access the second element
Array indexing starts at 0, soarr[1]is the second element, which is 20.Final Answer:
20 -> Option AQuick Check:
arr[1] = 20 [OK]
- Confusing index 1 with index 0
- Forgetting to use delete[] for arrays
- Expecting runtime error without cause
int *p = new int; *p = 5; delete p; delete p;
Solution
Step 1: Analyze pointer allocation and deletion
Pointerpis allocated memory and deleted once correctly.Step 2: Check for repeated delete
Deletingpa second time causes undefined behavior (double deletion error).Final Answer:
Double deletion of the same pointer -> Option CQuick Check:
delete twice = error [OK]
- Deleting pointer twice
- Not deleting allocated memory
- Confusing delete and delete[]
new. Which code correctly allocates and deallocates this array?Solution
Step 1: Allocate 2D array dynamically
int **arr = new int*[3]; for(int i=0; i<3; i++) arr[i] = new int[2]; // use arr for(int i=0; i<3; i++) delete[] arr[i]; delete[] arr; correctly creates an array of pointers for rows, then allocates each row as an array of ints.Step 2: Properly deallocate memory
Each row is deleted withdelete[], then the array of pointers is deleted withdelete[].Final Answer:
Uses an array of pointers, allocating each row separately -> Option BQuick Check:
Nested new and delete[] for 2D arrays [OK]
- Using delete instead of delete[] for arrays
- Not deleting all allocated rows
- Trying to allocate 2D array with single new incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
