1

I am developing an app in XCode and have to write a bit of C for an algorithm. Here is a part of the C code:

        double dataTag[M][N];

        // dataTag initialized to values.....

        double w[N]; // This is outside for loop at the top level of the method 
        for (int i = 0; i < N; i++) {
            w[i] = pow(10.0, dataTag[2][i] / 10.0 / b);
        }

        //This is inside for loop.....

        double disErr[N];

        // disErr set and values confirmed with printArray...

        double transedEstSetDrv[N][M];

        // transedEstSetDrv set and values confirmed with printArray...

        double stepGrad[M] = {0, 0, 0};
        for (int j = 0; j < M; j++) {

            double dotProductResult[M];
            dotProductOfArrays(w, disErr, dotProductResult, N);

            stepGrad[j] = sumOfArrayMultiplication(transedEstSetDrv[j], dotProductResult, M);
        }

        // Print array to console to confirm values
        NSLog(@"%f %f %f", stepGrad[0], stepGrad[1], stepGrad[2]); <-- if this is present algorithm gives different results.

        //Continue calculations......

So this is a part of algorithm in C which is inside for loop. The weird part is the NSLog that prints stepGrad array. Depending if i comment the call to the NSLog or not - the algorithm as a whole gives different results.

It would be great if someone gave some debugging suggestions.

Thanks!

UPDATE 1:

Simplified example which has the same issue and gave more code to support the issue.

UPDATE 2:

Removed the length_of_array function and just replaced it with a known number for simplicity.

  • 2
    You have a memory error. Probably a buffer overflow. You haven't included enough code to diagnoze the bug with certainty, but my guess is that `w` was passed as a parameter and that `LENGTH_OF_ARRAY(w)` doesn't work as you expect. See this answer http://stackoverflow.com/a/10349610/646887 – Klas Lindbäck Dec 01 '16 at 14:42
  • Could you give us an example of the output with and without printArray? – VitorMM Dec 01 '16 at 14:42
  • Hi guys thank you for quick comments. I have updated my answer and will take a look at the like. I compare the output values against the Matlab original implementation output and its latitude and longitude. I need at least 6 decimals precision after dot and it gives me only 5. And if i have that NSLog line - i get 7 that are matching if that makes sense – Marius Kurgonas Dec 01 '16 at 15:01
  • @MariusKurgonas i never worked with xcode but i know that printf's %f has a limit of 4-5 decimals (don't remember exactly) so if you print with only %f it's rounded even if the actual stored value is not. Maybe the fact that you get different results is just an "error" on those lines – John Doe Dec 01 '16 at 15:15
  • @JohnSmith Thanks for suggestion but i am comparing values later. This NSLog is just that i accidentally found this behaviour while trying to find out why the final values do not match, and they do if that call to NSLog is present... I have a suspicion that as far as code is in for loop and i do not explicitly set initial values for arrays they might point to random location after a cycle but yet to confirm it. Only one array is initialised explicitly that is in this example. – Marius Kurgonas Dec 01 '16 at 15:20
  • @MariusKurgonas i'm pretty sure that if you create a fixed size array it's content are automatically initialized to 0 so i don't think that would be the problem. doesn't husrt to try though – John Doe Dec 01 '16 at 15:25
  • 1
    Only global and static variables are initialized to 0. Auto variables (local variables) are not initialized to zero. – Klas Lindbäck Dec 01 '16 at 15:51

1 Answers1

1

So i will answer my own question.

Thanks to the comment from @Klas Lindbäck, i fixed the issue which was related to not initializing a C static array in for loop. So i went over all arrays before and after the code that had issue and did a

memset(a_c_array, 0, sizeof(a_c_array));

after declaration of each array. That is now working fine. Thank you for all your help!