Undefined , @ouah, , .
, ( ) (gcc, clang, msvc do)
.h, extern int a[4]; .c, int a[10];, , ( ).
.h , .
sizeof(a) == 10 * sizeof(int) .c, , sizeof(a) == 4 * sizeof(int) , .declaring.
:
foo.c:
#include <stdio.h>
int a[10];
void display();
int main() {
for(int i=0; i<sizeof(a)/sizeof(a[0]); i++) {
a[i] = i;
}
printf("sizeof(a)=%d\n", sizeof(a));
display();
return 0;
}
foo2.c:
#include <stdio.h>
extern int a[4];
void display() {
printf("sizeof(a)=%d\n", sizeof(a));
for(int i=0; i<sizeof(a)/sizeof(a[0]); i++) {
printf(" %2d", a[i]);
}
fputs("\n", stdout);
}
+ : cc foo.c foo2.c -o foo:
:
sizeof(a)=40
sizeof(a)=16
0 1 2 3
commons fortran, , C.
,
, , , , - , .
a , .o( .obj) . -, : C .
, , , Undefined. . :
C- ,
... [] , C C- , , - . , C...
[] (, , ), 256- . , , ... , , ... , .
, . , , ( , ). , , .
[The] affirm that the strong semantics of the memory model of the opportunity system (which provides protection against unobtrusive memory) can be maintained without sacrificing the benefits of a low-level language.
(underline mine)
TL / DR: Nothing prevents future compilers from adding size information for arrays inside a module (compiled) and raising an error if they are incompatible. There are currently studies for such functions.