Make sizeof() be of type size_t

This matters when sizeof is directly used in arithmetic,
ala "uintptr_t t; t &= -sizeof(long)" (for alignment).  When sizeof
isn't size_t (as it's specified to be) this masking will truncate
the high bits of the uintptr_t object (if uintptr_t is larger than
uint).
This commit is contained in:
Michael Matz
2012-04-16 01:13:25 +02:00
parent b068e29df7
commit 718fd591fa
4 changed files with 37 additions and 4 deletions

View File

@ -2155,6 +2155,8 @@ void c99_vla_test(int size1, int size2)
}
printf("\n");
#endif
}
typedef __SIZE_TYPE__ uintptr_t;
void sizeof_test(void)
@ -2175,6 +2177,20 @@ void sizeof_test(void)
printf("sizeof(a++) = %d\n", sizeof a++);
printf("a=%d\n", a);
ptr = NULL;
printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
/* The type of sizeof should be as large as a pointer, actually
it should be size_t. */
printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
uintptr_t t = 1;
uintptr_t t2;
/* Effectively <<32, but defined also on 32bit machines. */
t <<= 16;
t <<= 16;
t++;
/* This checks that sizeof really can be used to manipulate
uintptr_t objects, without truncation. */
t2 = t & -sizeof(uintptr_t);
printf ("%lu %lu\n", t, t2);
/* some alignof tests */