#include <iostream>
using namespace std;
int main(){
int* arr = new int[10];
for (int i=-9; i<=0; i++){
arr[i]=i;
}
delete [](arr);
system("pause");
}
Why does the delete[]
error occur when the array subscript is negative in the code below? We found that indexing an array to negative numbers is not a problem (arr[-1]
etc.) and arr
starting with index
from 0 will result in delete[]
without a problem What's the reason?ㅠ 에 The error content is _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
.
In the attached example, if you worked with a negative array subscript, you'd be lucky to work.
C/C++ can be done because it does not check the array subscripts, but if the process does not allocate memory for that address when indexed to a negative number, it terminates with a memory access error.
delete[] is a syntax that deallocates memory allocated through new[]. Therefore, an error is bound to occur because an arr that has never been allocated attempts to deallocate memory for a location corresponding to an index less than zero.
Experiments may or may not work depending on the system you are using, but these programs cannot be normal programs.
© 2024 OneMinuteCode. All rights reserved.