int main() {
int* i = new int(1);
i++;
*i=1;
delete i;
}
Here is my logic:
I increment I by 1, and then assign a value to it. Then
What your program shows is several cases of undefined behaviour:
*i = 1
)delete i + 1
.You MUST call delete
on exactly the same pointer-value that you got back from new
- nothing else. Assuming the rest of your code was valid, it would be fine to do int *j = i;
after int *i = new int(1);
, and then delete j;
. [For example int *i = new int[2];
would then make your i++; *i=1;
valid code]