Delete a pointer after the arithmetics

后端 未结 5 2027
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 06:28
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

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 07:10

    Who allocates is who deallocates. So you should not be able to delete something you did not new by yourself. Furthermore, i++;*i=1; is UB since you may access a restricted memory area or read-only memory...

    The code made no sense . I think You have XY problem. If you could post your original problem there will be more chance to help you.

提交回复
热议问题