While working on an unrelated problem, I stumbled across very surprising (to me) behavior of a C compiler. My code was the equivalent of the following:
#include <stdio.h>
int arr[42];
int main( void ) {
printf( "%u\n", sizeof( &arr ) );
return( 0 );
}
The & operator produces the address of its operand, and if the type of the operand is T, the type of the resulting expression is pointer to T, at least in C89 and later. So the size of that type really, really should be the size of a pointer. Here’s what Visual C++ 1.52c, the last 16-bit Microsoft compiler, declared as ANSI-compliant, does with it:
C:\temp>cl -W4 asz.c
Microsoft (R) C/C++ Optimizing Compiler Version 8.00c
Copyright (c) Microsoft Corp 1984-1993. All rights reserved.
asz.c
Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994
...
C:\temp>asz 84
84? That’s the size of the array, not the size of a pointer to the array. Needless to say, that is at odds not only with any modern compiler (gcc, clang) but also with many other mid-1990s compilers (IBM or Watcom for example). What was Microsoft doing there?
Continue reading →