在 LP64 环境中,sizeof 的有效类型为 size_t
,该类型会实现为 unsigned long
。有时,sizeof 会传递给需要使用类型为 int
的参数的函数,或者赋值给 int
类型的数据或强制转换为 int
类型。在某些情况下,这种截断可能会导致数据丢失。例如,
long a[50]; unsigned char size = sizeof (a);
将生成以下警告:
warning: 64-bit constant truncated to 8 bits by assignment warning: initializer does not fit or is out of range: 0x190