8

Is an array with 0 elements the same as an unallocated pointer?

Is int arr[0]; the same as int* arr;?

Edit: What if I did something similar to this:

int x[0];
int* const arr = x;

I tried this code and it compiled. To my knowledge, both x and arr should be pointing to the same location in memory. What would be the difference in this case?

Dasaru
  • 2,828
  • 5
  • 25
  • 25

3 Answers3

13

Not at all.

In the case of arr[0], arr has a well defined address.

In the case of *arr, arr is just uninitialized.

After your EDIT, where you initialize the const arr with an array defined just before : there would just be no difference in the content of the variables, but in the actions you would be allowed to perform on them.

Skippy Fastol
  • 1,745
  • 2
  • 17
  • 32
  • @Dasaru Did you try it? That doesn’t compile … – Konrad Rudolph Apr 20 '12 at 15:19
  • @Dasaru : normally you could ... but you'd have to initialize manually the **arr** to a given memory location, as it is const. Take a look at http://stackoverflow.com/questions/355258/why-must-const-members-be-intialized-in-the-constructor-initializer-rather-than – Skippy Fastol Apr 20 '12 at 15:27
  • 1
    In C++ it's not legal to have a zero length array, therefore the array will not have a well defined address. – bames53 Apr 20 '12 at 17:24
  • @bames53 : thanks for the clarification. Would you have the source of that info ? – Skippy Fastol Apr 21 '12 at 15:45
  • It comes from the C++ standard, [dcl.array] Chapter 8.3.4/1. "If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero." (The standard can be downloaded from [here](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/#mailing2012-01). Look for paper "n3337") There's actually more to it than just that quote, but it can't all be quoted here. – bames53 Apr 21 '12 at 16:53
4

A locally declared zero-length array is illegal in C++ so it's not the same as an unallocated pointer.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
0

A zero-length array points to a specific address, the beginning of the array. After the end of the array, you will have undefined data, in this case, at the address pointed to.

int arr[0];
int* ptr;

// arr is a reliable value;
// *arr is not;
// ptr is not;

One way this is useful: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Zero-Length.html

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • As far as I know there still is no consensus about whether this usage is actually legal according to the standard, since array access outside of bounds is simply UB (and *does* cause bugs in software, this is not merely a hypothetical). – Konrad Rudolph Apr 20 '12 at 15:37
  • @KonradRudolph In C90, there was no question: `int arr[0]` was illegal, including in a structure. And it was clear that if the array was declared `int arr[1];`, and you accessed with and index 2, it was undefined behavior. C99 (but not C++), you can declare the final member of a struct as `int arr[];` to support this usage (but `int arr[0];` remains an error). – James Kanze Apr 20 '12 at 16:58
  • @James Ah, that was it. So what about C++? I’m pretty sure that out-of-bounds array access was never legal, am I right? – Konrad Rudolph Apr 20 '12 at 18:30
  • @KonradRudolph Generally speaking, for the basic types, pointers and C style arrays, the intent of C++ is to be identical to C. (There is one exotic architecture which C++ formally forbid, but C allowed, at least in C++03. This was accepted as a defect, and has presumably been fixed in C++11, although I've not verified.) – James Kanze Apr 23 '12 at 08:14