-1

I'm relatively new to cpp, and I'm wondering if there's anyway to have visual studio code automatically write delete[] a line below whenever I type new, kinda similar to how it auto completes parentheses.

I think this would be very helpful for me as I have been learning about the heap and avoiding memory leaks.

LoserIRL
  • 29
  • 4
  • 10
    No. The way to solve the problem is to use types that do this for you like `std::vector`, `std::unique_ptr`, `std::shared_ptr` – NathanOliver Mar 10 '20 at 16:24
  • 4
    You should learn about RAII, containers and smart pointers next. You'll find that you hardly ever need (nor should you) to write new or delete in the first place. – eerorika Mar 10 '20 at 16:27
  • 2
    What would be the use case for this? Typically, when the lifetime of variable is limited by the scope they are in (when the behavior, that you are describing, would be useful) - you don't need `new` at all. – Algirdas Preidžius Mar 10 '20 at 16:35
  • 1
    Handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Mar 10 '20 at 16:45
  • 1
    That's not as helpful as you think – the most common use case (I'd guess around 99.9%) is to `delete` in a completely different part of the code. You should learn how to do without `new` and `delete` (it's quite rare to see those words at all these days). – molbdnilo Mar 10 '20 at 17:05

2 Answers2

2

I don't know much about Visual Studio in particular, but this is probably unlikely for a few reasons:

  1. What if you call new in one function and delete in a completely different one? (think constructor/destructor).
  2. What if you call new but pass ownership to another object, so they're then expected to delete?
  3. What if you call new on a bunch of different lines, but only have one delete that's in a loop?

If you want to make sure that each new has a matching delete however, I'd suggest using tools like CppCheck (to which I have no affiliation) on your code periodically (ie whenever you push new changes).

Or even better, use smart pointers or one of the many C++ containers like std::vector so you don't need to worry about this.

scohe001
  • 13,879
  • 2
  • 28
  • 47
1

I have been learning about the heap and avoiding memory leaks.

void foo();

void test()
{
    int* p = new int(24);
    foo();
    delete p;
}

Question: is the above code leak free?

Answer: No!! And whoever or whatever is teaching you that to avoid memory leaks you need to use delete is teaching you very very bad C++ programming. The above code will leak if foo() throws.

The only solution is to use RAII whenever you need to manage ownership of resources (like you do when you allocate memory). For this use smart pointers or higher abstractions like containers (std::vector etc).

So since almost always new/delete in user code is a bug there aren't tools to help you write new/delete.

bolov
  • 58,757
  • 13
  • 108
  • 182