Should I free a 0-size dynamic array?

Viewed 98

Hello I have a simple example: Since C++ allows a 0-size dynamic array of type T then how can I free it?

a = new int[0];
delete[] a;// UB?

As you can see above C++ compiler doesn't now about dynamic arrays size so it can be 0 so the code works. I also know that this array a works as past-end pointer so it cannot be neither de-referenced nor incremented/decremented.

  • I don't know what happens behind the curtain in operator new in this case? Does it allocate memory or not? or does it fail?

  • If I don't free this memory using delete[] a; is it a memory leak?

  • Can assign to it like: a = new int[10];?

0 Answers
Related