今天遇到了一例惯性问题。大家调用memset接口函数的时候,习惯性使用sizeof作为第二个参数,一般情况下,这样可以确保正确性。但是如果直接习惯性拿常数作为sizeof的输入,可能得不到期望的值。
char cmd[256];
memset( cmd, 0, sizeof( 256 ) );
[WARNING] (cwe=682, sizeofwithnumericparameter): Suspicious usage of ‘sizeof’ with a numeric constant as parameter., It is unusual to use a constant value with sizeof. For example, ‘sizeof(10)’ returns 4 (in 32-bit systems) or 8 (in 64-bit systems) instead of 10. ‘sizeof(‘A’)’ and ‘sizeof(char)’ can return different results.