Nikunj Banka
Nikunj Banka

Reputation: 11385

Very anomalous behaviours by C++ compiler

I have the following code. In the function xyz(int from, int to, int i). I am printing the value of i and i*2+1. But I am getting unexpected output with i = 1 and i*2+1 = -1. The function xyz2() is exactly the same except that I have uncommented a dummy function call and I am getting the expected output with i = 0 and i*2+1 = 1. Please see the output as I have explained it. Also I would mention that I get the same output on my local machine.

Why is this happening?

    #include <stdio.h>
    #include <stdlib.h>

    long long arr[2];
    long long xyz(int from, int to, int i);
    long long array[200000];
    long long xyz2(int from, int to, int i);

    long long foo(){return 141;}

    int main(){
        int n=2;
        arr[0] = -4;
        arr[1] = 5;
        xyz(0, 1, 0);
        printf("\n\n");
        xyz2(0, 1, 0);
        return 0;
    }

    long long xyz2(int from, int to, int i){
        if(from==to){
            return arr[to];
        }else{
            int mid = (from+to)/2;
            array[i*2+1] = xyz2(from, mid, i*2+1);
            array[i*2+1] = foo();
            printf("%d %d\n", (i*2)+1, i);
            return 100000;
        }
    }

    long long xyz(int from, int to, int i){
        if(from==to){
            return arr[to];
        }else{
            int mid = (from+to)/2;
            array[i*2+1] = xyz(from, mid, i*2+1);
            //array[i*2+1] = foo();   // The above function xyz2 gives the 
                                  //correct results on uncommenting this line
            printf("%d %d %d\n",array[i*2+1], (i*2)+1, i);
            return 100000;
        }
    }

Upvotes: 2

Views: 133

Answers (2)

FuzzyBunnySlippers
FuzzyBunnySlippers

Reputation: 3397

This line in xyz2(..):

  printf("%d %d %d\n",array[i*2+1], (i*2)+1, i);

is NOT the same as this line in xyz(..)

  printf("%d %d\n", (i*2)+1, i);

Changing the commented out line changed the result as follows:

With Line Commented

-4 1 0

1 0

With Line Uncommented

141 1 0

1 0

I don't seem to be getting any strange output (I think) using XCode 5....but I AM getting warnings about the format of the printed string:

printf("%d %d\n", (i*2)+1, i);

should be

printf("%lld %d\n", (i*2)+1, i);

Using long long requires a special print format, I believe. This could be the problem...

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110748

printf("%d %d %d\n",array[i*2+1], (i*2)+1, i);

The first data argument is a long long. You need to change your format string to match:

printf("%lld %d %d\n",array[i*2+1], (i*2)+1, i);

The reason you're getting weird behaviour when the wrong conversion specification is used is because the behaviour is undefined:

C99 §7.19.6.1/9 If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

This comes from the specification for fprintf, which printf is defined in terms of. The C99 standard is normative for C++11.

Upvotes: 6

Related Questions