Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 'new' and 'delete' getting deprecated in C++?

I stumbled upon a quiz that involved array declaration with different sizes. The first thing that came to my mind is that I would need to use dynamic allocation with the new command, like this:

while(T--) {    int N;    cin >> N;    int *array = new int[N];    // Do something with 'array'    delete[] array; } 

However, I saw that one of the solutions allowed the following case:

while(T--) {     int N;     cin >> N;     int array[N];     // Do something with 'array' } 

After a bit of research I read that g++ allows this, but it kept me thinking, in which cases is it then necessary to use dynamic allocation? Or is it that the compiler translates this as dynamic allocation?

The delete function is included. Note, however, that the question in here is not about memory leaks.

like image 872
learning_dude Avatar asked Jan 20 '20 10:01

learning_dude


People also ask

Can we use new and delete in C?

There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.

Should you use new and delete C++?

First, C++ is not garbage collected. Therefore, for every new, there must be a corresponding delete. If you fail to put this delete in, then you have a memory leak.

What happens when we use new and delete operator?

Memory that is dynamically allocated using the new operator can be freed using the delete operator. The delete operator calls the operator delete function, which frees memory back to the available pool. Using the delete operator also causes the class destructor (if one exists) to be called.

When using new what will happen if Delete is not used C++?

Using delete on a pointer to an object not allocated with new gives unpredictable results. You can, however, use delete on a pointer with the value 0. This provision means that, when new returns 0 on failure, deleting the result of a failed new operation is harmless.


1 Answers

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.

like image 180
Max Langhof Avatar answered Sep 19 '22 09:09

Max Langhof