c++ - CPP 检查器 : memsetValueOutOfRange

标签 c++ memset cppcheck

当我在 C++ 中使用 memset 函数时如下所示

char id[LIC_KEY_INFO_MAX_LEN + 1];

/* Create random numbers */
field[LIC_KEY_RANDOM] = createRand();

/* Create identifyer */
/* Padding in 0xff */
memset(&id, 0xffffffff, LIC_KEY_INFO_MAX_LEN);

然后我用 CPP 检查我的代码,它抛出警告:

Id: memsetValueOutOfRange
Summary: The 2nd memset() argument '4294967295' doesn't fit into an 'unsigned char'.
Message: The 2nd memset() argument '4294967295' doesn't fit into an 'unsigned char'. The 2nd parameter is passed as an 'int', but the function fills the block of memory using the 'unsigned char' conversion of this value.

我的问题是什么。 非常感谢。

最佳答案

你的问题正是代码检查器告诉你的, 0xffffffff 不适合 uchar,并且 memset 的第二个参数被评估为 uchar,因此传递它 0xff

关于c++ - CPP 检查器 : memsetValueOutOfRange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067946/

相关文章:

c++ - Cppcheck Cmake集成: abort build on warning

c++ - 架构 : plugin and standalone behavior

c++ - 初学者 C++ 问题

c++ - Windows 上不可读的文件属性

c - Memset 在外部函数中不起作用

c - 在 cppcheck 中越界访问缓冲区

c++ - 在 C++ 中创建两个 vector 之间的链接

c - 即使在正确转换后,增加 void 指针所需的左值

c++ - 是否应该在 memset 之前对结构进行类型转换

c++ - CppCheck。可以缩小变量的范围(和循环)