https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798
Bug ID: 110798 Summary: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) Product: gcc Version: og12 (devel/omp/gcc-12) Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: 13958014620 at 139 dot com Target Milestone: --- Some issues arised when doing some tests. The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is always 4 bytes(except char). The following example had run in fedora 37 OS with gcc_12.3.1 and gcc_13.1.0(compiled myself), both versions of gcc show the same issue. #include <cassert> int main(int argc, char ** argv){ int ret=0; typedef char ch_t; assert( sizeof(unsigned char) == 1); //right assert( sizeof(ch_t) == 1 ); //right assert( sizeof(unsigned ch_t) == 1); //the only one right typedef signed char sch; assert( sizeof(unsigned char) == 1); //right assert( sizeof(sch) == 1 ); //right assert( sizeof(unsigned sch) == 4); //wrong, which should be 1 typedef short sint; assert( sizeof(unsigned short) == 2 ); //right assert( sizeof(sint) == 2 ); //right assert( sizeof(unsigned sint) == 4); //wrong, which should be 8 typedef long lint; assert( sizeof(unsigned long) == 8); //right assert( sizeof(lint) == 8 ); //right assert( sizeof(unsigned lint) == 4); //wrong, which should be 8 typedef long long llint; assert( sizeof(unsigned long long) == 8); //right assert( sizeof(llint) == 8 ); //right assert( sizeof(unsigned llint) ==4); //wrong, which should be 8 /* The same errors as: 'typedef signed short ssint', 'typedef signed long slint', 'typedef signed long long sllint', 'typedef unsigned short usint', 'typedef unsigned long ulint', 'typedef unsigned long long ullint', so that seems all sizeof(unsigned <typedef-ed char/short/int/long type>) == 4, except typedef char ch_t; */ return ret; } ----------- or a version outputting the result on the terminal -------------- #include <iostream> #include <cassert> using namespace std; int main(int argc, char ** argv){ int ret=0; typedef char ch_t; cout << "typedef char ch_t;"<<endl; assert( sizeof(unsigned char) == 1); //right cout << "\tsizeof(unsigned char)=" << sizeof(unsigned char) <<endl; assert( sizeof(ch_t) == 1 ); //right cout << "\tsizeof(ch_t)=" << sizeof(ch_t) <<endl; assert( sizeof(unsigned ch_t) == 1); //the only one right cout << "\tsizeof(unsigned ch_t)=" << sizeof(unsigned ch_t) << " -- the only one right"<<endl; typedef signed char sch; cout << endl << "typedef signed char sch;"<<endl; assert( sizeof(unsigned char) == 1); //right cout << "\tsizeof(unsigned char)=" << sizeof(unsigned char) <<endl; assert( sizeof(sch) == 1 ); //right cout << "\tsizeof(sch)=" << sizeof(sch) <<endl; assert( sizeof(unsigned sch) == 4); //wrong, which should be 1 cout << "\tsizeof(unsigned sch)=" << sizeof(unsigned sch) << " -- wrong"<<endl; typedef short sint; cout << endl << "typedef short sint;" <<endl; assert( sizeof(unsigned short) == 2 ); //right cout << "\tsizeof(unsigned short)=" << sizeof(unsigned short int) <<endl; assert( sizeof(sint) == 2 ); //right cout << "\tsizeof(sint)=" << sizeof(sint) <<endl; assert( sizeof(unsigned sint) == 4); //wrong, which should be 8 cout << "\tsizeof(unsigned sint)=" << sizeof(unsigned sint) << " -- wrong"<<endl; typedef long lint; cout << endl << "typedef long lint;" <<endl; assert( sizeof(unsigned long) == 8); //right cout << "\tsizeof(unsigned long)=" << sizeof(unsigned long int) <<endl; assert( sizeof(lint) == 8 ); //right cout << "\tsizeof(lint)=" << sizeof(lint) <<endl; assert( sizeof(unsigned lint) == 4); //wrong, which should be 8 cout << "\tsizeof(unsigned lint)=" << sizeof(unsigned lint) << " -- wrong"<<endl; typedef long long llint; cout << endl << "typedef long long llint;" <<endl; assert( sizeof(unsigned long long) == 8); //right cout << "\tsizeof(unsigned long long)=" << sizeof(unsigned long long int) <<endl; assert( sizeof(llint) == 8 ); //right cout << "\tsizeof(llint)=" << sizeof(llint) <<endl; assert( sizeof(unsigned llint) ==4); //wrong, which should be 8 cout << "\tsizeof(unsigned llint)=" << sizeof(unsigned llint) << " -- wrong"<<endl; cout << endl << "The same errors as: \n\ 'typedef signed short ssint', \n\ 'typedef signed long slint', \n\ 'typedef signed long long sllint', \n\ 'typedef unsigned short usint', \n\ 'typedef unsigned long ulint', \n\ 'typedef unsigned long long ullint', \n\ so that seems all sizeof(unsigned <typedef-ed char/short/int/long type>) == 4, \n\ except typedef char ch_t;" << endl; return ret; } These issues can't reproduce in pure C language with GCC 12 and 13.