3

I have the following code, but in this line of code I have warning x[i] = (rhs[i] - x[i - 1]) / b;, compiler is telling me that rhs[i] is a garbage value. Why it's happend? And how to remove this warning?

double* getFirstControlPoints(double* rhs, const int n) {
    double *x;
    x = (double*)malloc(n * sizeof(double));
    double *tmp; // Temp workspace.
    tmp = (double*)malloc(n * sizeof(double));

    double b = 2.0;
    x[0] = rhs[0] / b;
    for (int i = 1; i < n; i++) // Decomposition and forward substitution.
    {
        tmp[i] = 1 / b;
        b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
        x[i] = (rhs[i] - x[i - 1]) / b; //The left operand of '-' is a garbage value
    }
    for (int i = 1; i < n; i++) {
        x[n - i - 1] -= tmp[n - i] * x[n - i]; // Backsubstitution.
    }
    free(tmp);
    return x;
}

enter image description here

enter image description here

All compiler warnings and calling getFirstControlPoints you may see on screenshots.

rowwingman
  • 5,558
  • 7
  • 32
  • 50

2 Answers2

1

You need a check to make sure you have at least 4 points in the points array because this loop (line 333):

for (NSInteger i = 1 ; i < n - 1 ; ++i) {
    // initialisation stuff
}

will not execute at all for n = 0, 1, 2.

Assume that points has 3 objects in it, At line 311 you set n to the count - 1 i.e. n == 2

Then the loop condition is i < 2 - 1 i.e. i < 1.

I think you need the loop condition to be i < n

JeremyP
  • 81,842
  • 15
  • 121
  • 161
0

if points.count is 0 or 1 you are facing some problems, because then, n is -1 or 0, and you access rhs[n-1]; and you malloc n* bytes;

maybe that can be the problem. that you put some rangechecks int o the code?

Peter Miehle
  • 5,888
  • 2
  • 36
  • 53
  • I add check `if (points.count > 1) {//add code from - (void)calculateCurvePointsForPoints:(NSArray *)points function}`, but nothing changed. But you right this check need to add. – rowwingman Aug 22 '12 at 13:15
  • Can you update the code and screenshots accordingly? That warning means the value may be uninitialized, but it's hard to figure out why the preprocessor thinks so without the exact code. – Analog File Aug 22 '12 at 14:25