当我使用调试模式构建代码时,res为0,正确。 但在我使用发布模式构建代码后,res为1。 即使我尝试使用#pragma指令来禁用编译器的最优化,但错误仍然存在。
这是代码:
#include <cstdio>
#define ulong unsigned __int64
void hashTransfer(const char * src, unsigned char hash[])
{
sscanf_s(src, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
&hash[0], &hash[1], &hash[2], &hash[3], &hash[4], &hash[5], &hash[6], &hash[7],
&hash[8], &hash[9], &hash[10], &hash[11], &hash[12], &hash[13], &hash[14], &hash[15],
&hash[16], &hash[17], &hash[18], &hash[19], &hash[20], &hash[21], &hash[22], &hash[23],
&hash[24], &hash[25], &hash[26], &hash[27], &hash[28], &hash[29], &hash[30], &hash[31]);
}
#pragma optimize("", off)
bool compartor(const unsigned char* lhs, const unsigned char* rhs)
{
const ulong* lhsP = (const ulong *)lhs;
const ulong* rhsP = (const ulong *)rhs;
ulong lhs4 = *(lhsP + 3);
ulong lhs3 = *(lhsP + 2);
ulong lhs2 = *(lhsP + 1);
ulong lhs1 = *(lhsP);
ulong rhs4 = *(rhsP + 3);
ulong rhs3 = *(rhsP + 2);
ulong rhs2 = *(rhsP + 1);
ulong rhs1 = *(rhsP);
//int a = lhs1 < rhs1;
//int b = (lhs1 == rhs1 && lhs2 < rhs2);
//int c = (lhs1 == rhs1 && lhs2 == rhs2 && lhs3 < rhs3);
//int d = (lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3 && lhs4 < rhs4);
//printf("\n%llx %llx\n", lhs1, rhs1);
//printf("%llx %llx\n", lhs2, rhs2);
//printf("%llx %llx\n", lhs3, rhs3);
//printf("%llx %llx\n", lhs4, rhs4);
return (lhs1 < rhs1)
|| (lhs1 == rhs1 && lhs2 < rhs2)
|| (lhs1 == rhs1 && lhs2 == rhs2 && lhs3 < rhs3)
|| (lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3 && lhs4 < rhs4);
}
#pragma optimize("", on)
int main()
{
unsigned char hash1[32];
hashTransfer("ff3f4036a1164d1ddbad5b3edf9022fddb3e1961a54a922708a6c1ffc49e5489", hash1);
unsigned char hash2[32];
hashTransfer("ff3f4036a1164d1ddbad5b3edf9022addb3e1961a54a922708a6c1ffc49e5489", hash2);
bool res = 0;
res = compartor(hash1, hash2);
printf("%d", res);
getchar();
return 0;
}
答案 0 :(得分:0)
Visual C ++产生的警告,即使使用默认警告级别字面,也会告诉您问题所在:
warning C4477: 'sscanf_s' : format string '%02x' requires an argument of type 'unsigned int *', but variadic argument 32 has type 'unsigned char *'
note: consider using '%hhx' in the format string