70

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.

14
  • 55
    The second example uses a variable-length array which have never been a part of C++. For this case use std::vector instead (std::vector<int> array(N);). Jan 20, 2020 at 10:04
  • 7
    The direct answer to your question should be: no, it is not getting deprecated. Even though modern versions of C++ provide many features simplifying memory ownership management (smart pointers), it is still common practice to allocate objects by invoking new OBJ directly.
    – pptaszni
    Jan 20, 2020 at 10:10
  • 9
    For other people who are confused about why people are talking about memory leaks, the question was edited to correct a bug that was not material to the question
    – Mike Caron
    Jan 20, 2020 at 20:20
  • 4
    @Mannoj prefer to use the terms Dynamic and Automatic to heap and stack. It's rare but it's possible to implement C++ without heaps and stacks. Jan 21, 2020 at 1:31
  • 2
    I'm not sure if that's a joke or not. C++ has officially deprecated a few things but they do tend to live on unofficially for one reason or another. Jan 21, 2020 at 20:37

7 Answers 7

118

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.

13
  • 3
    I would like to add that although VLAs are not in the standard officially, they are supported by all major compilers, and thus the decision of whether or not to avoid them is more of a matter of style/preference than a realistic concern for portability. Jan 21, 2020 at 4:41
  • 4
    Also remember that you can't return an array or store it elsewhere, so VLA won't ever outlive the function's execution time
    – Ruslan
    Jan 21, 2020 at 7:32
  • 16
    @StackTracer To the best of my knowledge, MSVC does not support VLAs. And MSVC is most definitely a "major compiler". Jan 21, 2020 at 8:44
  • 2
    "you have to always match a new with a delete " - not if you work with Qt, as its base classes all have garbage collectors, so you just use new and forget about it, most of the time. For GUI elements, when the parent widget is closed, the children go out of scope and are garbage collected automatically.
    – vsz
    Jan 21, 2020 at 12:33
  • 6
    @vsz Even in Qt, each new still has a matching delete; it's just that the deletes are done by the parent widget rather than in the same code block as the news.
    – jjramsey
    Jan 21, 2020 at 17:14
25

Well, for starters, new/delete are not getting deprecated.

In your specific case, they're not the only solution, though. What you pick depends on what got hidden under your "do something with array" comment.

Your 2nd example uses a non-standard VLA extension which tries to fit the array on the stack. This has certain limitations - namely limited size and the inability to use this memory after the array goes out of scope. You can't move it out, it will "disappear" after the stack unwinds.

So if your only goal is to do a local computation and then throw the data away, it might actually work fine. However, a more robust approach would be to allocate the memory dynamically, preferrably with std::vector. That way you get the ability to create space for exactly as many elements as you need basing on a runtime value (which is what we're going for all along), but it will also clean itself up nicely, and you can move it out of this scope if you want to keep the memory in use for later.

Circling back to the beginning, vector will probably use new a few layers deeper, but you shouldn't be concerned with that, as the interface it presents is much superior. In that sense, using new and delete can be considered discouraged.

3
  • 2
    Note the "...few layers deeper". If you were to implement your own containers, you should still avoid using new and delete, but rather use smart pointers like std::unique_pointer.
    – Max
    Jan 20, 2020 at 10:45
  • 3
    @Max : std::unique_ptr's default destructor calls delete or delete[], which means that the owned object must have been allocated by new or new[] anyway, which calls have been hidden into std::make_unique since C++14. Jan 21, 2020 at 11:48
  • Except, of course, that's the behavior of the default deleter. You can create a unique_ptr-based type with something other, and my goto FILE* handler for some time now is an incarnation of struct fcloser { void operator()(FILE* file) { std::fclose(file); }}; auto open(char const* path, char const* mode = "rb") { return std::unique_ptr<FILE, fcloser>{std::fopen(path, mode)}; } Jan 25, 2021 at 8:14
16

Your second examples uses variable length arrays (VLAs), which are actually a C99 (not C++!) feature, but nonetheless supported by g++.

See also this answer.

Note that variable length arrays are different from new/delete and do not "deprecate" them in any way.

Be also aware that VLAs are not ISO C++.

15

Modern C++ provides easier ways to work with dynamic allocations. Smart pointers can take care about the cleanup after exceptions (that may happen anywhere if allowed) and early returns, as soon as the referenced data structures go out of scope, so may make sense to use these instead:

  int size=100;

  // This construct requires the matching delete statement.
  auto buffer_old = new int[size];

  // These versions do not require `delete`:
  std::unique_ptr<int[]> buffer_new (new int[size]);
  std::shared_ptr<int[]> buffer_new (new int[size]); 
  std::vector<int> buffer_new (size);  int* raw_access = buffer_new.data();

From C++ 14 you can also write

auto buffer_new = std::make_unique<int[]>(size);

this looks even nicer and would prevent memory leak if the allocation fails. From C++ 20 you should be able to do as much as

auto a = std::make_shared<int[]>(size);

this for me still does not compile at the time of writing with gcc 7.4.0. In these two examples we also use auto instead of type declaration on the left. In all cases, use array as usual:

buffer_old[0] = buffer_new[0] = 17;

Memory leaks from new and crashes from doubled delete is something C++ has been bashed for many years, being the "central point" of argumentation for switching into other languages. Maybe better to avoid.

4
  • You should avoid the unique/shared_ptr constructors in favor of make_unique/shared, not only do you not have to write the constructed type twice (using auto) but you don't risk leaking memory or resources if construction fails partway (if you're using a type that can fail) Jan 24, 2020 at 3:59
  • 3
    make_unique is available with arrays from C++14, and make_shared from C++20 only. This is still seldom a default setting so proposing std::make_shared<int []>(size) looked for me somewhat ahead of time. Jan 24, 2020 at 7:48
  • Fair enough! I don't really use make_shared<int[]> much, when you pretty much always want vector<int>, but good to know. Feb 4, 2020 at 8:12
  • Excessive pedantry, but IIRC the unique_ptr constructor is nothrow, so for T that have nothrow constructors, so there's no risk of leaks with unique_ptr(new int[size]) and shared_ptr has the following: "If an exception is thrown, delete p is called when T is not an array type, delete[] p otherwise.", so you have the same effect - the risk is for unique/shared_ptr(new MyPossiblyAllocatingType[size]). Feb 4, 2020 at 8:29
3

new and delete are not getting deprecated.

The objects created by new operator can be passed by reference. The objects can be deleted using delete.

new and delete are the foundational aspects of the language. Persistence of an object can be managed using new and delete. These are definitely not going to be deprecated.

The statement - int array[N] is a way of defining an array. The array can be used within the scope of the enclosing code block. It cannot be passed like how an object is passed to another function.

2

The first example needs a delete[] at the end, or you will have a memory leak.

The second example use variable array length that is not supported by C++; it only allows constant-expression for array length.

In this case it is useful to use std::vector<> as solution; that wraps all the actions you can perform on an array into a template class.

7
  • 3
    What do you mean with "until C++11"? I'm pretty sure, VLAs never became part of the standard.
    – Lukas-T
    Jan 20, 2020 at 10:17
  • look at standard of c++14 [ c++14 standard ](isocpp.org/files/papers/N3690.pdf) on page 184 paragraph 8.3.4
    – Zig Razor
    Jan 20, 2020 at 10:23
  • 4
    Thats not the standard, but only a draft and the part about “arrays of runtime bound" did not make it into the standard as far as I can tell. cppreference does not mention VLAs there.
    – Lukas-T
    Jan 20, 2020 at 10:37
  • 1
    @zigrazor cppreference.com has a list of links to the closest drafts before/after the publication of each of the standards. The published standards are not freely available, but these drafts should be very close. As you can see from the document numbers, your linked draft is some older working draft for C++14.
    – walnut
    Jan 20, 2020 at 10:54
  • 2
    @learning_dude It is not supported by standards. The answer is (now) correct (albeit short). It only works for you because GCC allows it as a non-standard extension.
    – walnut
    Jan 21, 2020 at 7:53
-4

The syntax looks like C++, but the idiom is similar to plain old Algol60. It was common to have code blocks like this:

read n;
begin
    integer array x[1:n];
    ... 
end;

The example could be written as:

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

I sometimes miss this in the current languages ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.