My question are located in my code comment:
int* a = new int[0];// I've expected the nullptr according to my logic...
bool is_nullptr = !a; // I got 'false'
delete[] a; // Will I get the memory leaks, if I comment this row?
Thank you.
My question are located in my code comment:
int* a = new int[0];// I've expected the nullptr according to my logic...
bool is_nullptr = !a; // I got 'false'
delete[] a; // Will I get the memory leaks, if I comment this row?
Thank you.
For C++11, and given your code:
int* a = new int[0];
Zero is a legal size, as per 5.3.4/7:
When the value of the expression in a noptr-new-declarator is zero, the allocation function is called to allocate an array with no elements.
The operator invoked is as per 18.6.1.2 (emphasis mine):
void* operator new[](std::size_t size);
...
3 Required behavior: Same as for operator new(std::size_t). This requirement is binding on a replacement version of this function.
4 Default behavior: Returns operator new(size).
...referencing 18.6.1.1...
void* operator new(std::size_t size);
3 Required behavior: Return a non-null pointer to suitably aligned storage (3.7.4), or else throw a bad_- alloc exception. This requirement is binding on a replacement version of this function.
So, the pointer returned must be non-null.
You do need to delete[]
it afterwards.
In C++03 new int[0]
results in undefined behavior because the value between the []
must be a strictly positive value - zero is no good (5.3.4/6 "New"). So asking whether there's a memory leak afterwards is in a sense pointless.
In C++11 new int[0]
results in a call to the allocator to allocate a zero length array (5.3.4/7 "New"). If the allocation request succeeds, a pointer is returned - there's nothing in the standard that says how much memory the block pointed to by that pointer contains, other than it has to be at least the requested size. However, it has at least the effect of allocating at least one character, because that address cannot be returned by the allocator again until it has been freed. In practice, the bookkeeping overhead will be more than one character.
Yes, there is a leak, and it is not implementation-dependent.
This new expression cannot yield a null pointer. It allocates memory by calling operator new[]
, which is required to "return a non-null pointer to suitably aligned storage, or else throw a bad_alloc
exception" (see C++11 §18.6.1.1/3 and §18.6.1.2/3).
Further, the allocation function requirements (§3.7.4.1) require that each call to an allocation function returns a pointer that is distinct from all other pointers that have been allocated but not yet deallocated. Thus, the implementation cannot simply have a single "empty allocation" pointer that it always returns.
This, every array-form new expression allocates something, even if the extent is zero. If you don't deallocate that object via delete[]
, you have leaked it.
Yes, without delete
there will be a memory leak.
Every new
has to be paired with delete
. Even if the programmer-allocated size is 0. Allocator may allocate more memory than requested because of alignment requirements, management overhead or anything else.
In this case it is implementation defined whether you will be returned a you should be careful that you do not dereference this pointer also not calling nullptr
or not butdelete
will result in a Memory leak.
W.r.t to calling delete
the rule is simple:
"If you call new
you must call delete
."
Correction:
As the citations in other answers have made clear, It cannot return you a nullptr
.