sizeof() function result tests < -1

Richard Sharman richard_sharman@Mitel.COM
Fri Jan 7 10:57:00 GMT 2000


On a sparc and a 386,  although sizeof(int) returns 4 when used in an
assignment, the test  sizeof(int) < -1   returns true.


% gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs
gcc version 2.8.1
% uname -a
SunOS topaz 5.6 Generic_ sun4u unknown
% 

% cat -n x.c
     1	#include <stdio.h>
     2	
     3	int main(void) {
     4	  int	x;
     5	
     6	  /* This works as expected */
     7	  x = sizeof(int);
     8	  printf("%d\n", x);
     9	  if (x < -1) {
    10	    printf("x < -1\n");
    11	  } else {
    12	    printf("ok(1)\n");
    13	  }
    14	
    15	
    16	  /* This fails */
    17	  printf("%d\n", sizeof(int));
    18	  if (sizeof(int) < -1) {
    19	    printf("sizeof(int) < -1\n");
    20	  } else {
    21	    printf("ok(2)\n");
    22	  }
    23	
    24	
    25	  /* This, testing against 0, works as expected. */
    26	  printf("%d\n", sizeof(int));
    27	  if (sizeof(int) < 0) {
    28	    printf("sizeof(int) < 0\n");
    29	  } else {
    30	    printf("ok(3)\n");
    31	  }
    32	
    33	  exit(0);
    34	}
% gcc -Wall -o x x.c && ./x
4
ok(1)
4
sizeof(int) < -1
4
ok(3)
% 

The assembly code for lines 17 to 25 is:

.stabn 68,0,17,.LM9-main
.LM9:
	sethi %hi(.LLC0),%o1
	or %o1,%lo(.LLC0),%o0
	mov 4,%o1
	call printf,0
	nop
.stabn 68,0,19,.LM10-main
.LM10:
	sethi %hi(.LLC3),%o1
	or %o1,%lo(.LLC3),%o0
	call printf,0
	nop
.stabn 68,0,20,.LM11-main
.LM11:
	b .LL5
	nop
.LL4:
.LL4:
.stabn 68,0,21,.LM12-main
.LM12:
	sethi %hi(.LLC4),%o1
	or %o1,%lo(.LLC4),%o0
	call printf,0
	nop
.LL5:
.stabn 68,0,26,.LM13-main

And the relevant constants:

.LLC0:
.asciz	"%d\n"
.LLC3:
.asciz	"sizeof(int) < -1\n"

I repeated on a 586 under Linux and got the same results:

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i486-linux/2.7.2.3/specs
gcc version 2.7.2.3
$ uname -a
Linux cr420523-a 2.3.18 #3 Wed Oct 27 21:51:02 EDT 1999 i586 unknown
$ gcc -o x x.c
$ ./x
4
ok(1)
4
sizeof(int) < -1
4
ok(3)
$ 


More information about the Gcc-bugs mailing list