1

Could you give an example of such undefined behavior? I mean, there is a quote from 3.8/4:

For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.

I've been trying to invent my own example, but I've doubts about it. I think one does not properly reflect the rule I cited.

The code:

#include <iostream>
#include <typeinfo>

using std::cout;
using std::endl;

struct A
{
    ~A()
    {
        cout << "~A" << endl; //side-effect
    }

    A()
    { 
        cout << "A()" << endl;
    }
};

A *a = new A;


int main()
{
    //Prints A()
    //Is such behavior undefined?
}

The programm doesn't call destructor or operator delete explicitly. So, it must have UB. But it is strange if the programm has UB, because there is no explicitly destructor call and there is memory leak. Does memory leak produce UB?

  • [This answer](http://stackoverflow.com/a/24137427/1505939) specifically – M.M Sep 01 '14 at 05:18
  • Also relevant: http://stackoverflow.com/questions/9971559/why-is-not-deleting-an-object-that-has-a-destructor-with-a-side-effect-undefined – Brian Bi Sep 01 '14 at 05:22
  • @Brian good find, [my question](http://stackoverflow.com/questions/24137006/does-a-memory-leak-cause-undefined-behaviour) should have been merged with that, rather than the memory leak one . – M.M Sep 01 '14 at 05:27

0 Answers0