13

In C++11 it allows you to create a 0 length C array and std:array like this:

int arr1[0];
std::array arr2<int,0>;
  1. So I'm thinking what is the use of a array that doesn't have a space to store?
  2. Secondly what is the zero length array? If it is a pointer, where does it pointing to?
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 3
    https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html – itdoesntwork Oct 06 '14 at 02:07
  • 9
    No, C++ doesn't allow zero-length arrays. Even if it did, it wouldn't be a pointer. – Mike Seymour Oct 06 '14 at 02:09
  • 1
    @MikeSeymour: Above code peacefully compiles in `g++ version 4.8.3` with `-std=C++11` – Nayana Adassuriya Oct 06 '14 at 02:15
  • 1
    @NayanaAdassuriya add `-pedantic` flag and the first case will be flagged as an extension. – Shafik Yaghmour Oct 06 '14 at 02:16
  • 1
    @ Shafik Yaghmour: you means it is not in standard, even though compilers supports? – Nayana Adassuriya Oct 06 '14 at 02:17
  • 4
    @NayanaAdassuriya: Yes, many compilers (including GCC) have extensions. Compile with `-pedantic` (or even `-pedantic-errors`) if you want to stick to standard-compliant code. – Mike Seymour Oct 06 '14 at 02:18
  • 2
    An array of 0 length does have a pointer. Just don't try to write to it. – d3coy Oct 06 '14 at 02:24
  • @d3coy: Indeed, if such a thing were allowed, it would decay to a pointer. That pointer would be the one-past-the-end pointer. – Ben Voigt Oct 06 '14 at 02:54
  • @BenVoigt One past the end of what? – Neil Kirk Oct 06 '14 at 02:58
  • @NeilKirk: Past the end of nothing. (But, since such a thing doesn't exist, there's not much point in speculating what it would do if it did.) – Mike Seymour Oct 06 '14 at 03:15
  • @MikeSeymour I think it would be better to decay to a null pointer. – Neil Kirk Oct 06 '14 at 03:16
  • @NeilKirk: So that `(void*)arr` and `&arr` would have different addresses? That would be weird. But, since such a thing doesn't exist, there's not much point arguing about its properties. I'll leave that to the theologians. – Mike Seymour Oct 06 '14 at 03:18
  • @MikeSeymour Well it does exist, just not in Standard C++ – Neil Kirk Oct 06 '14 at 03:18
  • 2
    @MikeSeymour: There's no zero-length array **type**. However, there are definitely zero-length arrays: `new int[0]` is valid. – MSalters Oct 06 '14 at 09:16
  • Why was my comment deleted? – Neil Kirk Oct 06 '14 at 10:00
  • @NeilKirk: The rules for pointer arithmetic explicitly allow computing a pointer one past the end of the array, e.g. (`a + n` for an array of type `T[n]`). It doesn't point to an element within the array, so dereferencing it is undefined behavior, but it can be used for pointer arithmetic and comparisons. – Ben Voigt Oct 06 '14 at 13:09
  • Compiler support many extension, as I explained in my answer, compiling with `-pedantic` is the only way to make sure you know when you are using them. Linux takes advantage of many `gcc` extensions as do other projects which probably forces `clang` to support the same feature set. – Shafik Yaghmour Oct 17 '14 at 13:18

1 Answers1

20

Your first example is not standard C++ but is an extension that both gcc and clang allow, it is version of flexible arrays and this answer to the question: Are flexible array members really necessary? explains the many advantages of this feature. If you compiled using the -pedantic flag you would have received the following warning in gcc:

warning: ISO C++ forbids zero-size array 'arr1' [-Wpedantic]

and the following warning in clang:

warning: zero size arrays are an extension [-Wzero-length-array]

As for your second case zero-length std::array allows for simpler generic algorithms without having to special case for zero-length, for example a template non-type parameter of type size_t. As the cppreference section for std::array notes this is a special case:

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

It would also make it consistent with other sequence containers which can also be empty.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740