See this code here:

class test
{
    int n;
    int *j;
public:
    test(int m)
    {
        n = 12;
        j = new int; 
        cin >> *j;
    }

    void show()
    {
        cout << n << ' ' << *j << endl;
    }

    ~test()
    {
        delete j;
    }

};

int main()
{
    test var = 123;
    var.show();
    return 0;
}

In this program compiler should complain about double deleting of j. First delete is done when temporary object temporary(123) is destroyed. The second delete is done when var object is destroyed. But this is working fine?

Does it mean temporary object does not call destructor?

有帮助吗?

解决方案

The contentious line is this:

test var = 123;

The relevant standard text (that the pundits in the comments are referencing), I believe, is (8.5, "Declarators"):

The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is an rvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized;

Indeed, in 12.6, we get an example of this:

complex f = 3;  // construct complex(3) using
                // complex(double)
                // copy it into f

Thus, in your use of =, your implementation is probably directly constructing object and eliminating the intermediate temporary entirely (and, as the comments have noted, most do).

This class doesn't copy properly, so creating a copy of it (and the freeing the copy and the original) would result in a double delete (and crashes, undefined behavior, etc.). Because no copies are created, this scenario does not happen above.

其他提示

Two points: first, in this particular case, the compiler is allowed to optimize out the temporary, by an explicit authorization of the standard. All of the compilers I'm familiar with do. You can verify whether this is happening in your code by defining a copy constructor and instrumenting it.

Second is that if the temporary isn't optimized out, your code has undefined behavior. A double delete can have any imaginable behavior: an immediate crash, corruption of the free space arena (leading to a much later crash if the program continues running), no effect what so ever, or anything else. The fact that you are not seeing any symptoms doesn't mean that the code is correct.

The fact that the code happens to not blow up does not mean that it is correct.

Your class is buggy in that it is susceptible to double deletes in the manner you describe.

For example, changing var.show(); to the following:

    test(var).show();

makes the code reliably blow up on my computer.

To fix, implement the copy constructor and the assignment operator.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top