Are ‘new’ and ‘delete’ getting deprecated in C++?

Neither snippet you show is idiomatic, modern C++ code.

new and delete (and new[] and delete[]) are not deprecated in C++ and never will be. They are still the way to instantiate dynamically allocated objects. However, as you have to always match a new with a delete (and a new[] with a delete[]), they are best kept within (library) classes that ensure this for you. See Why should C++ programmers minimize use of ‘new’?.

Your first snippet uses a “naked” new[] and then never delete[]s the created array. That’s a problem. std::vector does everything you need here just fine. It will use some form of new behind the scenes (I won’t dive into implementation details), but for all you have to care, it’s a dynamic array but better and safer.

Your second snippet uses “variable length arrays” (VLAs), a C feature that some compilers also allow in C++ as an extension. Unlike new, VLAs are essentially allocated on the stack (a very limited resource). But more importantly, they are not a standard C++ feature and should be avoided because they are not portable. They certainly do not replace dynamic (i.e. heap) allocation.

Leave a Comment