0

If I allocate an array like this: int* arr = new int [n]; should I free memory using delete arr; or delete[] arr;. What's the difference between these two operators in this case (if any)?

  • 4
    Does [this](https://stackoverflow.com/questions/2425728/delete-vs-delete-operators-in-c) answer your question? – Nathan Pierson Dec 06 '21 at 18:29
  • In practice, don't allocate arrays like that though. Just use std::vector (it will do the memory managment for you) – Pepijn Kramer Dec 06 '21 at 18:42
  • `delete` pairs up with `new`, and `delete[]` pairs up with `new[]`. In modern C++, neither need to be used (unless you are making your own container or smart pointer). Instead use `std::vector` or `std::array` or `std::unique_ptr` (or if you have a really good need for it, `std::shared_ptr`). – Eljay Dec 06 '21 at 19:27

0 Answers0