Does calling new [] twice on the same pointer without calling delete [] in between cause a memory leak?

后端 未结 6 1000
天命终不由人
天命终不由人 2021-01-25 19:21

I\'ve heard that you should usually \"delete\" whenever you use \"new\", yet when I run a simple test program (below), it doesn\'t seem to make a difference which numbers I put

6条回答
  •  有刺的猬
    2021-01-25 20:11

    Yes. You don't call new on a pointer, you call new and it returns a pointer to the allocated memory. You are responsible for holding onto that pointer and deleting it. If you overwrite that value, as you are, you are leaking.

提交回复
热议问题