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

C语言中以硬编码数组返回多值是否正确?(用于minimax算法)

C函数返回两个值的正确方式与你的代码问题

你提出的返回硬编码数组的写法是错误的

你写的return {1,1}这种方式在C语言中属于未定义行为,原因如下:

  • 这种初始化列表会创建临时栈数组,函数执行完毕后,栈上的临时数据会被销毁。
  • 返回的指针指向已被释放的内存,后续访问该指针会导致程序崩溃、数据错乱等不可预知的问题。

针对你的Minimax算法,推荐几种正确实现方式

方式1:使用结构体封装返回值(最推荐)

将需要返回的最佳列和分数封装成结构体,直接返回结构体实例,无需处理指针和内存问题,简单安全。

示例代码:

// 定义返回结果的结构体
typedef struct {
    int bestCol;
    int score;
} MinimaxResult;

MinimaxResult minimax(int ** board, int depth, bool maxPlayer){
    MinimaxResult result;
    int newscore;
    int * validLocations= getValidLocations(board);
    bool gameOver= gameOver(board);

    if (depth==0 || gameOver){
        if (gameOver){
            if(isWin(board, COMPUTER)){
                result.bestCol = -1;
                result.score = 10000;
            } else if(isWin(board,PLAYER)){
                result.bestCol = -1;
                result.score = -10000;
            } else{
                result.bestCol = -1;
                result.score = 0; // 平局
            }
        } else { // depth为0
            result.bestCol = -1;
            result.score = boardScore(AI);
        }
        free(validLocations); // 释放getValidLocations分配的内存
        return result;
    }

    if(maxPlayer){
        int val= INT_MIN;
        int bestCol= validLocations[0];
        int validLocationsIndex=0;
        int col= validLocations[0];

        while (col!=-1  && validLocationsIndex<7){
            int ** possibleBoard= copyBoard(board);
            insert(possibleBoard, col, COMPUTER);
            MinimaxResult childResult = minimax(possibleBoard, depth-1, false);
            newscore= childResult.score;
            freeBoard(possibleBoard);

            if (newscore>val){
                val= newscore;
                bestCol= col;
            }
            validLocationsIndex++;
            col= validLocations[validLocationsIndex];
        }
        // 循环结束后再返回,避免提前终止遍历
        result.bestCol = bestCol;
        result.score = val;
        free(validLocations);
        return result;
    } else {
        int val= INT_MAX;
        int bestCol= validLocations[0];
        int validLocationsIndex=0;
        int col= validLocations[0];

        while (col!=-1  && validLocationsIndex<7){
            int ** possibleBoard= copyBoard(board);
            insert(possibleBoard, col, PLAYER);
            MinimaxResult childResult = minimax(possibleBoard, depth-1, true);
            newscore= childResult.score;
            freeBoard(possibleBoard);

            if (newscore<val){
                val= newscore;
                bestCol= col;
            }
            validLocationsIndex++;
            col= validLocations[validLocationsIndex];
        }
        result.bestCol = bestCol;
        result.score = val;
        free(validLocations);
        return result;
    }
}

方式2:通过指针参数输出结果

在函数参数中传入两个指针,让函数直接把结果写入指针指向的内存,无需返回复杂类型。

示例代码片段:

void minimax(int ** board, int depth, bool maxPlayer, int * outBestCol, int * outScore){
    // ... 逻辑处理 ...
    // 直接给指针赋值
    *outBestCol = bestCol;
    *outScore = val;
    // ...
}

// 调用方式
int bestCol, score;
minimax(board, depth, true, &bestCol, &score);

方式3:动态分配内存(需注意内存释放)

使用malloc在堆上分配数组,返回指针,但调用者必须记得用free释放内存,否则会导致内存泄漏。

示例代码:

int * minimax(int ** board, int depth, bool maxPlayer){
    int * result = malloc(2 * sizeof(int));
    if (!result) {
        // 内存分配失败处理
        return NULL;
    }
    // ... 逻辑处理 ...
    result[0] = bestCol;
    result[1] = score;
    return result;
}

// 调用方式
int * res = minimax(board, depth, true);
if (res) {
    int bestCol = res[0];
    int score = res[1];
    free(res); // 必须释放内存
}

你的Minimax代码还有其他问题需要修正

  • 循环内提前return:你在while循环的第一次迭代就执行了return,导致程序无法遍历所有有效列,无法找到真正的最优解,需将return移到循环结束之后。
  • 内存泄漏:getValidLocations返回的指针未被释放,需在函数结束前调用free(validLocations)(假设getValidLocations用malloc分配内存)。

内容的提问来源于stack exchange,提问作者Diana Farhat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 14:05:47