# Why does the program end in a crash?

**URL:** <https://scilab.discourse.group/t/why-does-the-program-end-in-a-crash/500>\
**Category:** Dynamic link\
**Created:** [May 6, 2024, 6:27pm UTC](https://scilab.discourse.group/t/why-does-the-program-end-in-a-crash/500 "2024-05-06T18:27:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zoow](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zoow](https://scilab.discourse.group/u/Zoow)\
**Post date:** [May 6, 2024, 6:27pm UTC](https://scilab.discourse.group/t/why-does-the-program-end-in-a-crash/500/1 "2024-05-06T18:27:51Z")

</div>

```auto
#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);

```auto

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

```auto

```

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [May 7, 2024, 7:43am UTC](https://scilab.discourse.group/t/why-does-the-program-end-in-a-crash/500/2 "2024-05-07T07:43:35Z")

</div>

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):

> [@Zoow](#):
>
> ```C
> double a, b;
> scilab_getDouble(env, in[0], &a);
> scilab_getDouble(env, in[0] + 1, &b);
> 
> ```

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

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

```

S.
