C语言中return语句未终止函数反而回到while循环判定条件的问题
C语言return语句未正常终止函数问题
我有一段C语言代码,当执行以下代码段时:
if (idx - 1 == 0) { return opening_address; }
程序并未终止当前函数并返回结果,反而回到了while循环的判定条件处。这段代码确实会被执行,而且循环中的另一条return语句也存在相同异常行为——本质上相当于执行了continue操作。
完整代码如下:
int find_smallest_free_block(int block_size) { int opening_address = 0; int idx = 0; while (idx + lowestPower <= highestPower) { if (powers[idx].hasHoles) { if (powers[idx].size > block_size) { opening_address = powers[idx].head_p->start_address; // 向下一级添加两个空闲块 int try = 0; try = addHole(idx - 1, powers[idx].head_p->start_address, powers[idx].head_p->end_address / 2); try = addHole(idx - 1, powers[idx].head_p->end_address / 2, powers[idx].head_p->end_address); // 移除当前级的空闲块 try = removeHole(idx, powers[idx].head_p->start_address); if (idx - 1 == 0) { return opening_address; // 最低层级无法再拆分空闲块 } idx -= 1; continue; } else { return opening_address; } } idx += 1; } return opening_address; }
内容的提问来源于stack exchange,提问作者David Rohweder
相关产品推荐
相关产品推荐

