History

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).

Michael Matz committed 14 years ago

Commit 718fd591 parent b068e294 files · +37 −4Browse files

Showing the changes to tests (1 of 4 files). Show all

Unified · Split

tests/tcctest.c+16−0
@@ -2155,6 +2155,8 @@ void c99_vla_test(int size1, int size2) · 2154 unmodified lines
21552155 #endif
21562156 }
21572157
2158+typedef __SIZE_TYPE__ uintptr_t;
2159+
21582160 void sizeof_test(void)
21592161 {
21602162 int a;
@@ -2175,6 +2177,20 @@ void sizeof_test(void) · 14 unmodified lines
21752177 ptr = NULL;
21762178 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
21772179
2180+ /* The type of sizeof should be as large as a pointer, actually
2181+ it should be size_t. */
2182+ printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2183+ uintptr_t t = 1;
2184+ uintptr_t t2;
2185+ /* Effectively <<32, but defined also on 32bit machines. */
2186+ t <<= 16;
2187+ t <<= 16;
2188+ t++;
2189+ /* This checks that sizeof really can be used to manipulate
2190+ uintptr_t objects, without truncation. */
2191+ t2 = t & -sizeof(uintptr_t);
2192+ printf ("%lu %lu\n", t, t2);
2193+
21782194 /* some alignof tests */
21792195 printf("__alignof__(int) = %d\n", __alignof__(int));
21802196 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t