You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:10:57