18

This discussion came up in comments on another post.

Sample code:

#include <string>

void func()
{
    std::string* foo;
    foo = new std::string[125];

    throw 1;

    delete [] foo;
}

int main()
{
     try { func(); }
     catch(int x) {}
}

Does this program cause undefined behaviour? It was claimed that it is UB under §3.8 p4, which is:

A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. 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.

Community
  • 1
  • 1
M.M
  • 130,300
  • 18
  • 171
  • 314
  • Oh, so there is. That's mostly a duplicate although it does discuss using deleted pointers which is a completely separate issue. – M.M Jun 10 '14 at 09:16
  • 4
    I think http://stackoverflow.com/questions/21110059/observable-behavior-and-undefined-behavior-what-happens-if-i-dont-call-a-des?rq=1 is a better duplicate candidate here. – ecatmur Jun 10 '14 at 12:33

0 Answers0