MiddleDebuggingCommonNot answered yet
What's wrong with delete arr after new int[100]?
This allocation and release look paired, but the program has a latent bug.
int* arr = new int[100];
// ... use arr ...
delete arr; // released here
Find and fix the bug.
Undefined behavior: memory from new[] must be released with delete[], not delete. Mismatching them is UB — typically a leak or heap corruption. Fix: write delete[] arr, or avoid raw arrays via std::vector<int> or std::make_unique<int[]>(100).
- ✗Assuming
deleteanddelete[]are interchangeable - ✗Thinking the mismatch only matters for types with destructors
- ✗Expecting the compiler to catch the mismatch
- →Why does
delete[]need to know the element count whendeletedoes not? - →How does
std::make_unique<int[]>(n)remove this risk entirely?