new operator in C++ - Time & Space Complexity
When we use the new operator in C++, it creates space in memory for data. Understanding how long this takes helps us write faster programs.
We want to know: How does the time to create new memory grow as we ask for more?
Analyze the time complexity of the following code snippet.
int* arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
}
This code creates an array of size n using new and then fills it with values.
Look at what repeats in this code.
- Primary operation: The loop that sets each array element.
- How many times: Exactly
ntimes, once for each element.
As n grows, the number of steps to fill the array grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to fill |
| 100 | About 100 steps to fill |
| 1000 | About 1000 steps to fill |
Pattern observation: The work grows directly with the size of the array.
Time Complexity: O(n)
This means the time to fill the array grows in a straight line with the number of elements.
[X] Wrong: "Using new to create an array is instant and does not depend on size."
[OK] Correct: While new allocates memory quickly, filling or initializing each element takes time proportional to the number of elements.
Knowing how memory allocation and initialization scale helps you explain program speed clearly and shows you understand how computers handle data.
"What if we replaced the loop with a function that copies values from another array? How would the time complexity change?"
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
