Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Weird compiler behavior on LeetCode

I was solving LeetCode problem: 7. Reverse Integer, for testing purposes I was printing some values (long long x2 and long long x3). Here, value of xRough were assigned to x2 and x3 if x or xRough is negative, otherwise an undefined value will be assined. But, LeetCode compiler assigns value of xRough to x2 and x3 even if x is positive (instead of assigning an undefined value). I tested this code on different compilers and they gave undefined values of x2 and x3 when x is positive. But LeetCode compiler is giving the wrong result.

Isn’t this wrong or weird behavior of the LeetCode compiler or am I missing some point here?

class Solution {
public:
    int reverse(int x) {

        long long x2, x3;
        long long xRough = (long long) x;
        
        if (xRough < 0){
            x2 = x3 = (-1) * xRough;
        }
        
        cout<<x2<<" "<<x3<<" "<<xRough<<endl;
        
        return 0;

    }
};

I have also got Accepted with this weird behavior of compiler. The accepted code is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Solution {
public:
    int reverse(int x) {

        long long flag = 0, y, x2, x3, xRough = (long long) x;
        long long theNum = 0, i = 1;
        
        if (xRough < 0)
            x2 = x3 = std::abs(xRough);
         
        while (1) {
            x2 = x2 / 10;

            i = i * 10;

            if (x2 == 0)
                break;
        }
        i = i / 10;

        while (1) {
            y = x3 % 10;
            x3 = x3 / 10;

            theNum = theNum + y * i;
            i = i / 10;

            if (x3 == 0)
                break;
        }

        if (x < 0) {
            if (theNum > std::pow(2, 31))
                return 0;
            else
                return (-1) * theNum;
        } else {
            if (theNum > (std::pow(2, 31) - 1))
                return 0;
            else
                return theNum;
        }

    }
};

>Solution :

In your function:

class Solution {
public:
    int reverse(int x) {

        long long x2, x3;
        long long xRough = (long long) x;
        
        if (xRough < 0){
            x2 = x3 = (-1) * xRough;
        }
        
        cout<<x2<<" "<<x3<<" "<<xRough<<endl;
        
        return 0;

    }
};

There are two cases one has to distinguish. Either xRough < 0 is true or it is false.

When it is true x2 will get a value assigned and everything works as you expect.

When it is false then the code tries to print the values of x2 and x3, but they are not initialized. They have indeterminate values. You cannot do anything with indeterminate values without invoking undefined beahvior.

The only way the function has defined output is when xRough < 0 is true. When it is false the output can be anything. Hence, completely ignoring the case of xRough < 0 being false is allowed for the compiler. Thats what compilers are built for, thats why they are good in optimizing code, they can eliminate paths in the code that are never taken (or may never be taken because the outcome is undefined anyhow).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading