Reputation: 352
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,
Upvotes: 1
Views: 917
Reputation: 11
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.
Upvotes: 1
Reputation: 46
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
Upvotes: 2
Reputation: 3432
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
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.
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.
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.
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.
Upvotes: 1