好的,
我编写了一个函数,该函数从十六进制文件中获取一个无符号字符,然后将其向左移动以适合 WORD、DWORD 或 QWORD,如下所示:
retVal |= ((unsigned char)(memory[i]) << (8 * j));
(在循环内,因此变量 i 和 j)。
现在视觉工作室让我想起了可能的算术溢出。
我的问题:如果我将 j 限制为不超过 8(uint64_t 的大小),我可以安全地忽略此消息吗?我总是对警告感到有些沮丧,并试图消除它们。
据我了解,在保存价值之前向左移动多少并不重要,我弄错了吗?
编辑:
这是一个例子(这是我的功能):
int getValuePNTR(const char* memory, int &start, int size)
{
uint64_t retVal = 0;
//now just add up array fields
for (int i = start + size-1,j = size-1; j >= 0; --j, i--)
{
//fprintf(stdout, "\ncycle: %d, memory: [%x]", j, memory[i]);
if ((unsigned char)memory[i] == 00 && j > 0)
retVal <<= 8;
else
retVal |= ((unsigned char)(memory[i]) << (8 * j));
}
//get the next field after this one
start += size;
return retVal;
}