Why is it impossible to build a compiler that can determine if a C++ function will change the value of a particular variable?

后端 未结 13 735
感动是毒
感动是毒 2021-01-30 02:01

I read this line in a book:

It is provably impossible to build a compiler that can actually determine whether or not a C++ function will change the val

相关标签:
13条回答
  • 2021-01-30 02:28

    There are multiple avenues to explaining this, one of which is the Halting Problem:

    In computability theory, the halting problem can be stated as follows: "Given a description of an arbitrary computer program, decide whether the program finishes running or continues to run forever". This is equivalent to the problem of deciding, given a program and an input, whether the program will eventually halt when run with that input, or will run forever.

    Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist.

    If I write a program that looks like this:

    do tons of complex stuff
    if (condition on result of complex stuff)
    {
        change value of x
    }
    else
    {
        do not change value of x
    }
    

    Does the value of x change? To determine this, you would first have to determine whether the do tons of complex stuff part causes the condition to fire - or even more basic, whether it halts. That's something the compiler can't do.

    0 讨论(0)
  • 2021-01-30 02:31

    I don't think it's necessary to invoke the halting problem to explain that you can't algorithmically know at compile time whether a given function will modify a certain variable or not.

    Instead, it's sufficient to point out that a function's behavior often depends on run-time conditions, which the compiler can't know about in advance. E.g.

    int y;
    
    int main(int argc, char *argv[]) {
       if (argc > 2) y++;
    }
    

    How could the compiler predict with certainty whether y will be modified?

    0 讨论(0)
  • 2021-01-30 02:32

    Imagine such compiler exists. Let's also assume that for convenience it provides a library function that returns 1 if the passed function modifies a given variable and 0 when the function doesn't. Then what should this program print?

    int variable = 0;
    
    void f() {
        if (modifies_variable(f, variable)) {
            /* do nothing */
        } else {
            /* modify variable */
            variable = 1;
        }
    }
    
    int main(int argc, char **argv) {
        if (modifies_variable(f, variable)) {
            printf("Modifies variable\n");
        } else {
            printf("Does not modify variable\n");
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-30 02:32

    To make the question more specific I suggest the following set of constraints may have been what the author of the book may have had in mind:

    1. Assume the compiler is examining the behavior of a specific function with respect to const-ness of a variable. For correctness a compiler would have to assume (because of aliasing as explained below) if the function called another function the variable is changed, so assumption #1 only applies to code fragments that don't make function calls.
    2. Assume the variable isn't modified by an asynchronous or concurrent activity.
    3. Assume the compiler is only determining if the variable can be modified, not whether it will be modified. In other words the compiler is only performing static analysis.
    4. Assume the compiler is only considering correctly functioning code (not considering array overruns/underruns, bad pointers, etc.)

    In the context of compiler design, I think assumptions 1,3,4 make perfect sense in the view of a compiler writer in the context of code gen correctness and/or code optimization. Assumption 2 makes sense in the absence of the volatile keyword. And these assumptions also focus the question enough to make judging a proposed answer much more definitive :-)

    Given those assumptions, a key reason why const-ness can't be assumed is due to variable aliasing. The compiler can't know whether another variable points to the const variable. Aliasing could be due to another function in the same compilation unit, in which case the compiler could look across functions and use a call tree to statically determine that aliasing could occur. But if the aliasing is due to a library or other foreign code, then the compiler has no way to know upon function entry whether variables are aliased.

    You could argue that if a variable/argument is marked const then it shouldn't be subject to change via aliasing, but for a compiler writer that's pretty risky. It can even be risky for a human programmer to declare a variable const as part of, say a large project where he doesn't know the behavior of the whole system, or the OS, or a library, to really know a variable won't change.

    0 讨论(0)
  • 2021-01-30 02:38

    As soon as a function calls another function that the compiler doesn't "see" the source of, it either has to assume that the variable is changed, or things may well go wrong further below. For example, say we have this in "foo.cpp":

     void foo(int& x)
     {
        ifstream f("f.dat", ifstream::binary);
        f.read((char *)&x, sizeof(x));
     }
    

    and we have this in "bar.cpp":

    void bar(int& x)
    {
      foo(x);
    }
    

    How can the compiler "know" that x is not changing (or IS changing, more appropriately) in bar?

    I'm sure we can come up with something more complex, if this isn't complex enough.

    0 讨论(0)
  • 2021-01-30 02:43

    I think the key word in "whether or not a C++ function will change the value of a particular variable" is "will". It is certainly possible to build a compiler that checks whether or not a C++ function is allowed to change the value of a particular variable, you cannot say with certainty that the change is going to happen:

    void maybe(int& val) {
        cout << "Should I change value? [Y/N] >";
        string reply;
        cin >> reply;
        if (reply == "Y") {
            val = 42;
        }
    }
    
    0 讨论(0)
提交回复
热议问题