Is it necessary to call delete[] vs delete for char arrays?

前端 未结 3 2000

I\'m utilizing a library written by a collegue and discovered that valgrind was spewing out errors related to the delete.

The problem was t

相关标签:
3条回答
  • 2021-01-12 08:29

    Forget about destructors. The difference between new/delete and new[]/delete[] is that these are two completely unrelated, independent memory allocation mechanisms. They cannot be mixed. Using delete to deallocate memory allocated with new[] is no different than using free for the same purpose.

    0 讨论(0)
  • 2021-01-12 08:32

    The standard says nothing about how the memory will get deleted -- it merely says that to not match the correct new with the correct delete is undefined behavior.

    Actually, new[] followed by delete usually frees all the memory that was allocated with new[], but destructors for the items in that array are not called correctly. (Most of the time -- not that the standard mandates that)

    Rather than dynamically allocated arrays, you should consider use of vector.

    0 讨论(0)
  • 2021-01-12 08:36

    If you allocate an array using new[], you have to destroy it using delete[]. In general, the functions operator delete(void*) and operator delete[](void*) aren't guaranteed to be the same.

    Refer here

    0 讨论(0)
提交回复
热议问题