简体   繁体   中英

Segmentation fault

In C, when I try to run this program, I get a "Segmentation fault". What does it mean? How can I fix this?

Tag tagNewDataPoint(const double x[MAX_DIMENSION],
                    const double w[MAX_DIMENSION],
                    const int d)
{
    int separator_arr,point_arr;
    double result = 0;
    for (separator_arr=0;separator_arr<d;separator_arr++)
    {
        for (point_arr=0;point_arr<d;separator_arr++)
        {
            result += w[separator_arr]*x[point_arr];
        }
    }

    if (result <0)
    {
        return NEG;
    }
    else if (result >0)
    {
        return POS;
    }
    else
    {
        return NOTAG;
    }
}

This:

for (point_arr=0;point_arr<d;separator_arr++)

should be:

for (point_arr=0;point_arr<d;point_arr++)

You increment the separator_arr , but checks the pointer_arr value (which is never changed) soon enough separator_arr is too big, and your address is invalid.

You have index crosstalk.

for (point_arr=0;point_arr<d;separator_arr++)

should be

for (point_arr=0;point_arr<d;point_arr++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM