1

I just stumbled upon the fact that we can dynamically create arrays without 'new' (Dynamic array without new (C++)). For example, this is possible:

cout<<"enter length: ";
cin>>length;

int a[length];

My question is: since we didn't use 'new' to dynamically allocate the memory, should we (or can we) use 'delete' in order to destroy the object? thanks,

user3353185
  • 127
  • 1
  • 12
  • there is no garbage. – 463035818_is_not_an_ai May 29 '21 at 13:44
  • 3
    Does this answer your question? [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai May 29 '21 at 13:44
  • 2
    _"I just stumbled upon the fact that we can dynamically create arrays without 'new"_ - no, you can't. This is an compiler specific extension with a tendency to blow up the stack when not used with care. In modern C++ I would recommend to not use this. – Lukas-T May 29 '21 at 13:46
  • 1
    actually this answer to the question you link already answers your question indirectly: https://stackoverflow.com/a/25475733/4117728. `int a[lenght]` is not a dynamic array. It is cleaned up just like `int x;` – 463035818_is_not_an_ai May 29 '21 at 13:46
  • 1
    fwiw, don't confuse `new` and `delete` in C++ with garbage collection. C++ has RAII which is a whole different concept than garbage collection. In some sense C++ needs no garbage collection, because there is no garbage to be collected – 463035818_is_not_an_ai May 29 '21 at 13:48
  • I realise that is a more sensible container to use. It's just that I stumbled upon this feature by mistake (because of my old Python habits) and was curious about whether I needed to delete an array if I created it this way. Didn't know it was a compiler-specific feature. Thanks for all the responses. – user3353185 May 30 '21 at 11:09

3 Answers3

2

In C++, there is no garbage collection. We need to delete all the items that were allocated on the heap. In your code, when you create an array without new, you don't allocate any space on the heap, you just use limited stack. You should avoid the new keyword if it isn't necessary, but if you need to use it, don't forget to use delete for each item you allocated space for on the heap. For your question, you don't need to delete the objects if you didn't allocate space on the heap for it.

int a[length]; // allocates an array on the stack
int *a = new int[length]; // allocates an array on the heap
He3lixxx
  • 3,263
  • 1
  • 12
  • 31
Berk Kırtay
  • 46
  • 1
  • 3
  • Actually garbage collection is allowed in C++, but none of main C++ compiler inplement it. Just for theoretical curiosity - you might start reading here: https://en.cppreference.com/w/cpp/memory/gc/declare_reachable – PiotrNycz May 29 '21 at 14:23
  • In addition to that C++11 gc, C++ also has smart pointers, which is a form of garbage collection. – Aykhan Hagverdili May 29 '21 at 17:26
1

You create an int a [size] array that is pushed onto the function stack and will be removed when the function ends. Calling the delete operator is not required. Your array isn't dynamic.

From the cplusplus.com tutorial on operator new:

Dynamic memory is allocated using operator new. new is followed by a data type specifier and, if a sequence of more than one element is required, the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated.

In most cases, memory allocated dynamically is only needed during specific periods of time within a program; once it is no longer needed, it can be freed so that the memory becomes available again for other requests of dynamic memory.

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
1

Should you have to ask?

I don't think you should use this feature. The c++ way of allocating a variable-length, contiguous list of elements is using std::vector<int>. This will give you nice, portable code, and you will not have to care about compiler-dependent behavior, stack size, or any other limitation.

But, if you still want to ask, we first have to decide

Who to ask?

The other answers imply that this is a question about c++, the standardized language. It is not. As the comments have noted, the code you wrote is not standard compliant c++. It makes little sense to reason about it with concepts from the standard when they don't apply*.

This code will only compile with a compiler that provides you an extension that allows you to have variable length arrays. Thus, you have to ask your compiler manufacturer.

gcc

If you are using gcc/g++, their specification is the relevant document, and in their case, it says:

The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.

So: No, you do not have to manually delete or free this memory.

clang

If you are using clang, this page states:

However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs.

So, you can expect their extension to behave compatible to C99, which means you also don't have to manually free anything.

msvc

This question indicates msvc does not support VLAs, so here, you also do not need to manually free anything.


* Of course, when adding a extensions with semantics that look like you're using a local variable, it makes sense that this variable also behaves like a local variable, so from the people developing gcc, I'd expect this to be consistent with the rest of the behavior in c++, but in theory, compiler manufacturers can build their compilers as strange as they want, and they could also make this extension expect you to manually free this memory.

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
  • Hi, thanks for your response. I agree that is a more sensible container to use. It's just that I stumbled upon this feature by mistake (because of my old Python habits) and was curious about whether I needed to delete an array if I created it this way. Didn't know it was a compiler-specific feature that's why I asked it here. – user3353185 May 30 '21 at 11:03
  • 1
    Oh, I think your question is a very good one, and should be answered, I just wanted to make sure that you and other people who come across this question understand that this is a compiler extension, that's why I put that "disclamer" at the start of my answer. I tried to rephrase the first paragraph to sound a little nicer. Did it help? If not, feel free to edit it in any way that you think would be appropriate. – He3lixxx May 30 '21 at 11:17