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

C语言中如何安全返回可变长度的多项式根结果?

安全返回多项式根的API设计方案

针对二次多项式求根场景(返回0、1或2个实根),以下几种API设计方式能安全传递结果,避免调用方出现越界访问:

1. 返回包含根数量的结构体

定义一个结构体封装根的数量和存储根的数组(最多2个元素),调用方必须先读取根的数量,再访问对应索引的根,从设计层面杜绝越界。

以C语言为例:

typedef struct {
    int count;  // 根的数量:0、1或2
    double roots[2];
} QuadraticRoots;

QuadraticRoots find_roots(double a, double b, double c) {
    QuadraticRoots result = {0};
    double discriminant = b*b - 4*a*c;
    if (discriminant > 0) {
        result.count = 2;
        result.roots[0] = (-b + sqrt(discriminant))/(2*a);
        result.roots[1] = (-b - sqrt(discriminant))/(2*a);
    } else if (discriminant == 0) {
        result.count = 1;
        result.roots[0] = -b/(2*a);
    }
    // 判别式小于0时count保持0,无实根
    return result;
}

调用示例:

QuadraticRoots roots = find_roots(1, -3, 2);
if (roots.count == 2) {
    printf("两个根:%f, %f\n", roots.roots[0], roots.roots[1]);
} else if (roots.count == 1) {
    printf("一个根:%f\n", roots.roots[0]);
} else {
    printf("无实根\n");
}

2. 通过输出参数传根,返回根的数量

将根作为指针参数传入函数,函数返回值直接表示找到的根的个数。调用方根据返回值决定是否访问对应的输出参数。

C语言示例:

int find_roots(double a, double b, double c, double* root1, double* root2) {
    double discriminant = b*b - 4*a*c;
    if (discriminant > 0) {
        *root1 = (-b + sqrt(discriminant))/(2*a);
        *root2 = (-b - sqrt(discriminant))/(2*a);
        return 2;
    } else if (discriminant == 0) {
        *root1 = -b/(2*a);
        return 1;
    }
    return 0;
}

调用示例:

double r1, r2;
int count = find_roots(1, -3, 2, &r1, &r2);
switch(count) {
    case 2:
        printf("两个根:%f, %f\n", r1, r2);
        break;
    case 1:
        printf("一个根:%f\n", r1);
        break;
    default:
        printf("无实根\n");
}

3. 返回动态分配数组,搭配输出参数传数量

适用于支持动态内存的语言,函数返回动态分配的根数组,同时用额外指针参数传出根的数量。需注意调用方必须负责释放内存,API文档需明确说明这一点。

C语言示例:

#include <stdlib.h>
#include <math.h>

double* find_roots(double a, double b, double c, int* count) {
    *count = 0;
    double discriminant = b*b - 4*a*c;
    double* roots = NULL;
    if (discriminant > 0) {
        *count = 2;
        roots = malloc(2 * sizeof(double));
        roots[0] = (-b + sqrt(discriminant))/(2*a);
        roots[1] = (-b - sqrt(discriminant))/(2*a);
    } else if (discriminant == 0) {
        *count = 1;
        roots = malloc(sizeof(double));
        roots[0] = -b/(2*a);
    }
    return roots;
}

调用示例:

int count;
double* roots = find_roots(1, -3, 2, &count);
if (count == 2) {
    printf("两个根:%f, %f\n", roots[0], roots[1]);
} else if (count == 1) {
    printf("一个根:%f\n", roots[0]);
} else {
    printf("无实根\n");
}
free(roots); // 必须手动释放内存

4. 利用语言原生的可变长度容器/可选类型

在Python、C++、Java等现代语言中,可直接利用语言特性返回可变长度容器(如Python列表、C++ std::vector),容器的长度就是根的数量;或用可选类型处理单根/无根的情况,天然避免越界问题。

Python示例:

import math

def find_roots(a, b, c):
    discriminant = b**2 - 4*a*c
    if discriminant > 0:
        root1 = (-b + math.sqrt(discriminant))/(2*a)
        root2 = (-b - math.sqrt(discriminant))/(2*a)
        return [root1, root2]
    elif discriminant == 0:
        return [-b/(2*a)]
    else:
        return []

调用示例:

roots = find_roots(1, -3, 2)
if len(roots) == 2:
    print(f"两个根:{roots[0]}, {roots[1]}")
elif len(roots) == 1:
    print(f"一个根:{roots[0]}")
else:
    print("无实根")

内容的提问来源于stack exchange,提问作者Ter Maxima

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 02:41:05