Subject: undefined behaviour in boost help
From: Sandor <[email-masked]>
Date: 1 Apr 2005 21:39:59 -0500
Newsgroups: comp.lang.c++.moderated

I found the following code example in the Boost library documentation at 
  http://boost.org/libs/utility/in_place_factories.html

struct C
{
    C() : contained_(0) {}
    C ( X const& v ) : contained_ ( new X(v) ) {}

    template<class InPlaceFactory>
    C ( InPlaceFactory const& aFactoty )
:
     contained_ ( uninitialized_storage() )
    {
      aFactory.template apply<X>(contained_);
    }

   ~C() { delete contained_ ; }

   X* uninitialized_storage() { return static_cast<X*>(new 
char[sizeof(X)]) ; }

   X* contained_ ;
} ;

void foo()
{
   C c( in_place(123,"hello" ) ;
}

I believe that the "delete contained_" expression causes undefined 
behaviour, because the contained_ pointer sometimes points to an area 
allocated with new[]. Such areas must be deleted with delete[]. Am I 
correct? (It seems quite unfortunate to increase confusion with putting 
undefined-behaviour code into the docs.)

cheers,
Sandor

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]