Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the zero-length array mentioned in the draft standard?

I was reading the draft standard N3337 and footnote 79 (§ 5.3.7 page 110) states (emphasis mine):

79) For non-zero-length arrays, this is the same as a pointer to the first element of the array created by that new-expression. Zero-length arrays do not have a first element

I was under the impression that C++ did not support zero-length arrays.

like image 858
Glenn Teitelbaum Avatar asked Nov 17 '15 13:11

Glenn Teitelbaum


People also ask

What is a zero length array?

Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type.

What is zero size array in C?

Zero length arrays are described as being an IBM Extension enabled by the *EXTENDED compiler option (aka -qlanglvl=extended when using ixlc). According to the compiler output *EXTENDED is in effect (which is why char d[]; is being accepted as a flexible array).

What is length of an array?

Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array. Syntax: public int Length { get; } Property Value: This property returns the total number of elements in all the dimensions of the Array.

Can we declare an array of size zero?

Zero-length array declarations are not allowed, even though some compilers offer them as extensions (typically as a pre-C99 implementation of flexible array members).


2 Answers

While zero length arrays like

int arr[0];

are not standard C++, dynamic arrays of length zero like

int *arr = new int[0];

are legal and are what your quote is referring too:

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.

5.3.4 (7) in N3337.

like image 79
Baum mit Augen Avatar answered Oct 11 '22 00:10

Baum mit Augen


As far as I understand this is to allow new to be implemented in terms of malloc which allows zero sized requests. You can not do much with them since as noted in the quote below dereferencing such a pointer is undefined behavior.

We can find a rationale in the draft C++ standard footnote 35 which is referenced from section 3.7.4.1 [basic.stc.dynamic.allocation]:

[...]Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a non-null pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The effect of dereferencing a pointer returned as a request for zero size is undefined.35

and footnote 35 says:

The intent is to have operator new() implementable by calling std::malloc() or std::calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.

From the C11 draft standard section 7.22.3 Memory management functions:

[...]If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

like image 39
Shafik Yaghmour Avatar answered Oct 11 '22 01:10

Shafik Yaghmour