IF语句可以评估错误吗?

时间:2014-05-24 12:57:57

标签: c if-statement gdb infinite-loop evaluation

我的C技能充其量生锈。 但是对于我的生活,我无法理解为什么评估为true的评估语句不会跳进块中。

    if(buffer[left_start+k].structure.id < buffer[l].structure.id) {
                    if(c1 < num_structs1) {
                        outputbuffer[opcount] = buffer[left_start+k]; 
                        fread((buffer+left_start+k), 1, sizeof(structure), tempfp1);
                        k++;
                        c1++;
                        opcount++;
                        counter++;
                    }

当单步执行gdb时,来自

230                     if(buffer[left_start+k].structure.id < buffer[l].structure.id) {
(gdb) n
241                     else if(buffer[left_start+k].structure.id > buffer[l].structure.id)
(gdb) n
242                         if(c2 < num_structs2) { .....


(gdb) p buffer[left_start+k].structure.id
$7 = 11
(gdb) p buffer[l].structure.id
$8 = 2608
(gdb) p (buffer[left_start+k].structure.id > buffer[l].structure.id)
$9 = 0

有人可以解释这种未定义的行为吗?

编辑:

块本身看起来像这样

else if(ftype == 0) {
if(buffer[left_start+k].structure.id < buffer[l].structure.id) {
    if(c1 < num_structs1) {
        outputbuffer[opcount] = buffer[left_start+k]; 
        fread((buffer+left_start+k), 1, sizeof(structure), tempfp1);
        k++;
        c1++;
        opcount++;
        counter++;
    }
}
else if(buffer[left_start+k].structure.id > buffer[l].structure.id) {
    if(c2 < num_structs2) {
        outputbuffer[opcount] = buffer[l];
        fread((buffer+l), 1, sizeof(structure), tempfp2);
        l++;
        c2++;
        opcount++;
        counter++;
    }
}   

}

这是从第229行开始的步骤

    229                 else if(ftype == 0) {
(gdb) n
230                     if(buffer[left_start+k].structure.id < buffer[l].structure.id) {
(gdb) p buffer[left_start+k].structure.id
$13 = 32
(gdb) p buffer[l].structure.id
$14 = 2613
(gdb) p (buffer[left_start+k].structure.id < buffer[l].structure.id)
$15 = 1
(gdb) n
241                     else if(buffer[left_start+k].structure.id > buffer[l].structure.id) {

匪徒本身,一个包含两个结构的联盟。

$23 = {structure = {sname = "Irqua\000mi", '\000' <repeats 30 times>, obj = 3, width = 8, uid = 87847, id = 32}, tstructure = {
    tname = "Irqua\000mi", '\000' <repeats 31 times>, "\003\000\000\000\b\000\000\000'W", id = 2097153}}
(gdb) p bufferArray[l]
$24 = {structure = {sname = "Oisoslaey\000m", '\000' <repeats 27 times>, obj = 4, width = 8, uid = 375318, id = 2613}, 
  tstructure = {tname = "Oisoslaey\000m", '\000' <repeats 28 times>, "\004\000\000\000\b\000\000\000\026\272", id = 171245573}}

编辑: 这是源代码的链接,以及与之关联的数据文件,只需编译并在不带参数的情况下运行, http://tinyurl.com/par63xr P.S对代码感到抱歉......

编辑: 所以我打破了循环,虽然我没有修复无限循环,将opcount和counter的增量移动到块的外部到if(低&lt; high)else if(low&gt; high)的末尾。 对于未定义的行为,我仍然没有答案。任何人都可以详细说明如何移动这两个增量可以改变循环的评估方式吗?

0 个答案:

没有答案