Common pointer mistakes in C++ - Time & Space Complexity
When working with pointers, it is important to understand how operations involving them affect program speed.
We want to see how the time to run code changes as the input size grows when pointers are used.
Analyze the time complexity of the following code snippet.
void printArray(int* arr, int size) {
for (int i = 0; i < size; i++) {
std::cout << *(arr + i) << " ";
}
std::cout << std::endl;
}
This code prints all elements of an integer array using a pointer.
- Primary operation: Loop that accesses each element via pointer arithmetic.
- How many times: Exactly once for each element in the array (size times).
As the array size grows, the number of pointer accesses grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pointer accesses and prints |
| 100 | 100 pointer accesses and prints |
| 1000 | 1000 pointer accesses and prints |
Pattern observation: The work grows evenly as the input size grows.
Time Complexity: O(n)
This means the time to run the code grows in direct proportion to the number of elements.
[X] Wrong: "Using pointers makes the code run instantly or in constant time regardless of input size."
[OK] Correct: Even with pointers, you still need to visit each element to process it, so time grows with input size.
Understanding how pointer operations scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we changed the loop to process only every other element? How would the time complexity change?"
Practice
int *p;int x = *p;Solution
Step 1: Understand pointer initialization
The pointerpis declared but not initialized, so it points to an unknown memory location.Step 2: Dereferencing uninitialized pointer
Dereferencing*ptries to access memory at an undefined address, causing undefined behavior or crash.Final Answer:
It causes undefined behavior and may crash the program. -> Option AQuick Check:
Uninitialized pointer dereference = crash [OK]
- Assuming uninitialized pointers are null
- Thinking dereferencing gives zero
- Ignoring pointer initialization
Solution
Step 1: Understand null pointer initialization
In modern C++,nullptris the preferred way to initialize a pointer to null.Step 2: Check other options
NULLis old style (macro),0x0is a literal but less clear, and'0'is a char, not a pointer.Final Answer:
int *p = nullptr; -> Option BQuick Check:
Use nullptr for null pointers [OK]
- Using character '0' instead of nullptr
- Using integer 0 instead of nullptr
- Confusing NULL macro with nullptr
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
Solution
Step 1: Understand pointer assignment
Pointerpstores the address ofx. So*prefers tox.Step 2: Modify value through pointer
Assigning*p = 20;changes the value ofxto 20.Final Answer:
20 -> Option DQuick Check:
Pointer dereference changes original variable [OK]
- Confusing pointer value with pointed value
- Expecting original variable unchanged
- Thinking *p prints address
int *p = nullptr;
std::cout << *p << std::endl;
Solution
Step 1: Analyze pointer initialization
Pointerpis initialized tonullptr, meaning it points to nothing.Step 2: Dereferencing null pointer
Trying to access*pcauses a runtime error (crash) because it points to no valid memory.Final Answer:
Dereferencing a null pointer causes runtime error. -> Option CQuick Check:
Dereference nullptr = crash [OK]
- Assuming nullptr can be dereferenced safely
- Ignoring runtime crash possibility
- Confusing pointer declaration with usage
int *p = new int(5);
delete p;
// What should be done next?
Solution
Step 1: Understand delete effect
Afterdelete p;, the pointerpstill holds the old address but memory is freed (dangling pointer).Step 2: Prevent dangling pointer
Settingp = nullptr;makes it clear pointer is invalid and safe to check before use.Final Answer:
Set p to nullptr after delete. -> Option AQuick Check:
Nullify pointer after delete to avoid dangling [OK]
- Using delete[] for single object
- Not setting pointer to nullptr after delete
- Assuming pointer auto-clears after delete
Ready to prove it?
Pass the test and this pattern moves unknown
