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,589
  • 7
  • 33
  • 50
  • I have removed the `objective-c` tag from this question because this is a pure `C` question. – Jonathan Grynspan Aug 22 '12 at 12:45
  • 1
    I can't see anything to suggest that it would be garbage in the function. How are you calling it? (although I've no idea if that would affect anything) – James M Aug 22 '12 at 12:46
  • 1
    gcc doesn't give any error or warning, as long as you compile in C99 or C++ mode. Maybe the error is coming from somewhere else? Can you post the exact error message? What compiler are you using? – onitake Aug 22 '12 at 12:46
  • @JonathanGrynspan Re-added, because now there's objective C as well. – James M Aug 22 '12 at 13:07
  • As you can see I add all code where I calling getFirstControlPoints(). So as you I understand all arrays are initialised. – rowwingman Aug 22 '12 at 13:10
  • The arrays might be correctly allocated, but it looks like they're not fully filled with data. Check thoroughly if you are assigning all elements. _edit_: Ok, it _looks_ like you're initializing it completely... – onitake Aug 22 '12 at 13:15
  • @JamesMcLaughlin: Fair enough. – Jonathan Grynspan Aug 22 '12 at 13:17
  • To silence the code analyzer, you could always use `calloc(n, sizeof(double)`, but that is only a workaround... – onitake Aug 22 '12 at 13:20
  • Is this at runtime or compile time? What compiler? What is the exact warning including warning number? – Ben Aug 22 '12 at 13:28
  • This is at compiler time. Apple LLVM 4.0 – rowwingman Aug 22 '12 at 13:42
  • @onitake the output is from the clang static analyser, not the compiler. – JeremyP Aug 22 '12 at 14:20

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
  • 84,577
  • 15
  • 123
  • 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,984
  • 2
  • 38
  • 55
  • 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