【发布时间】:2015-04-20 21:38:40
【问题描述】:
我有两个大小相同的 unsigned char 数组和一个 if 语句来检查它们是否相等:
#define BUFFER_SIZE 10000
unsigned char origChar[BUFFER_SIZE];
unsigned char otherChar[BUFFER_SIZE];
//Yes, I know this is unnecessary
memset(origChar,'\0',BUFFER_SIZE);
memset(otherChar,'\0',BUFFER_SIZE);
. . .
if(memcmp(origChar,otherChar,offset))
{
. . .
}
当我检查 gdb 中的两个数组时,我得到以下信息:
(gdb) p origChar
$1 = '\000' <repeats 9999 times>
(gdb) p otherChar
$2 = '\000' <repeats 9999 times>...
(gdb) p memcmp(otherChar,origChar,offset)
$3 = 1
但是,如果我将 offset 减 1,我会得到以下结果:
(gdb) p memcmp(otherChar,origChar,offset-1)
$4 = 0
(gdb) p offset
$5 = 10000
这对我来说真的没有任何意义。 GDB 基本上说它们是完全相等的,那为什么将offset 减一会改变呢?
【问题讨论】:
-
为什么
offset与BUFFER_SIZE不同? -
offset的值是要改变的,但在这种情况下,BUFFER_SIZE和offset的值是 10000。 -
offset的值是多少?BUFFER_SIZE的值是多少? -
@Sky 现在的问题是——你的程序真的做了什么?只是 gdb 的问题,还是您的程序实际上返回了错误的结果?
-
这里没有问题:ideone.com/38MTc6