Adam frank
Adam frank

Reputation: 1

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;
    }
}

Upvotes: 0

Views: 233

Answers (2)

user786653
user786653

Reputation: 30480

You have index crosstalk.

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

should be

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

Upvotes: 2

MByD
MByD

Reputation: 137412

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.

Upvotes: 5

Related Questions