Why does the program end in a crash?

#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include <math.h>

static const char fname[] = "sci_foo6";

double f(double x) {
    // Define your function f(x) here
    return x * x - 2; // Example: solving the equation x^2 - 2 = 0
}

double df(double x) {
    // Define the derivative of your function f(x) here
    return 2 * x; // Example: derivative of the function x^2 - 2
}

int sci_foo6(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt* opt, int nout, scilabVar* out)
{
    if (nin != 1 || nout != 1) {
        Scierror(77, _("%s: Incorrect number of input or output arguments: expected 1 input and 1 output.\n"), fname);
        return 1;
    }

    if (scilab_isDouble(env, in[0]) == 0 || scilab_isVector(env, in[0]) == 0 ||
        scilab_isComplex(env, in[0]) == 1) {
        Scierror(999, _("%s: Incorrect type for input argument. Expected a vector of two numbers.\n"), fname);
        return 1;
    }

    int row_col[2];
    scilab_getDim2d(env, in[0], &row_col[0], &row_col[1]);
    if (row_col[1] != 2) {
        Scierror(999, _("%s: Incorrect type for input argument. Expected a vector of two numbers.\n"), fname);
        return 1;
    }

    double a, b;
    scilab_getDouble(env, in[0], &a);
    scilab_getDouble(env, in[0] + 1, &b);

    double fa = f(a);
    double fb = f(b);

    if (fa * fb > 0) {
        Scierror(999, _("%s: No roots on the specified interval.\n"), fname);
        return 1;
    }

    const int max_iter = 100; // Maximum number of iterations
    const double tol = 1e-6;  // Tolerance

    for (int iter = 0; iter < max_iter; ++iter) {
        double c = (a + b) / 2;
        double fc = f(c);

        if (fabs(fc) < tol || (b - a) / 2 < tol) {
            // Solution found with the specified tolerance or interval too small
            out[0] = scilab_createDouble(env, c);
            return 0;
        }

        if (fa * fc < 0) {
            b = c;
            fb = fc;
        } else {
            a = c;
            fa = fc;
        }
    }

    // If solution couldn't be found
    Scierror(999, _("%s: Couldn't find a solution on the specified interval with the specified tolerance.\n"), fname);
    return 1;
}

Compile:

ilib_build(“liboverload”,[“sci_foo6”,“sci_foo6”, “csci6”],“foo6.c”,,“”,“”,“”);
exec(“loader.sce”);
input_vector = [0, 2];
solution = sci_foo6(input_vector);


Conclusion:

Scilab has detected a fatal error.
Please check that your user-defined functions (or external modules) appear in the stack trace.

  • sample code that reproduces the problem;
  • the result of the instruction [a, b] = getdebuginfo();
  • the following information:
    [Zoow:21301] Signal: Segmentation Error (11)
    [Zoow:21301] Signal code: Address not mapped (1)
    [Zoow:21301] Failing at address: 0x200007e22

Hello,

Welcome on Sciab discourse forum!

A priori, the problem is here, as in[0]+1 is not a valid address of anything, and because the first call tries to get the whole vector (and you give only a 1-size placeholder a):

Since in[0] is a Scilab array of Double, you have to get it like this

double a, b;
double x[2];
scilab_getDouble(env, in[0], x);
a = x[0];
b = x[1];

S.