Global integer array without dimension

What is the concept when we define a global array without a dimension This shows the result as 16.

#include <stdio.h> #include <stdlib.h> int arr[]; int main(int argc, char *argv[]) { arr[1] = 16; printf("%d\n",arr[1]); system("PAUSE"); return 0; } 

And even sizeof (arr) doesn't work. Why?

+6
source share
2 answers

int arr[]; is a conditional definition.

Clause 6.9.2, Clause 2 states:

Declaring an identifier for an object with a file region without an initializer and without a storage class specifier or using a static element of a storage class is a preliminary definition. If a translation unit contains one or more preliminary definitions for an identifier, and a translation unit does not contain an external definition for this identifier, then the behavior is exactly the same as if the translation unit contained a declaration of the scope of this identifier, a composite type at the end of the translation unit, with an initializer equal to 0 .

and example 2 in paragraph 5 of this section clarifies:

If at the end of the translation block containing

  int i[]; 

array i is still of an incomplete type; the implicit initializer forces it to have one element, which is set to zero when the program starts.

So, at the end of the translation unit, your arr array is of type int[1] . To the end, its type is incomplete, so sizeof does not work, since in main the array type is still not complete.

Access to arr[1] causes undefined behavior, since arr has only one element.

+12
source

GCC assumes that arr should have only one element. The fact that you can access elements other than arr[0] without segfault is just a coincidence. For example, on my machine, I can access arr[1] , arr[10] and arr[100] just fine, but arr[1000] calls segfault. In general, access to locations outside the bounds of the array causes undefined behavior.

+1
source

Source: https://habr.com/ru/post/947861/


All Articles