Thursday, February 19, 2015

Digest for comp.lang.c++@googlegroups.com - 4 updates in 3 topics

Robert Wessel <robertwessel2@yahoo.com>: Feb 19 11:32AM -0600

On Wed, 18 Feb 2015 09:29:15 +0100, Torsten Mueller
 
>In my application I use also boost (1.57). And boost has an own cstdlib
>header. Could this be the reason?
 
>Has anyone had this problem in the last time, malloc is undefined?
 
 
Could you be seeing a namespace problem? While cstdlib mostly
includes stdlin.h, it does put most items into the std: namespace.
Perhaps the headers got tightened up recently to not put those
functions in both std: and the global namespace?
Ian Collins <ian-news@hotmail.com>: Feb 19 05:37PM +1300

Stefan Ram wrote:
 
> int main()
> { ::std::unique_ptr< c >o( f( &o )); o->print(); }
 
> Does this program violate any rule of C++?
 
I'd pick passing the address of an uninitialised object (o) to f to be UB.
 
By the way, why to you keep adding superfluous scoping, but remove the
white-space that would make the code readable? I'm sure there are some
obscure corner cases that may justify the scoping, but Usenet posts
aren't one of them!
 
--
Ian Collins
Paavo Helde <myfirstname@osa.pri.ee>: Feb 18 10:46PM -0600

ram@zedat.fu-berlin.de (Stefan Ram) wrote in
 
> int main()
> { ::std::unique_ptr< c >o( f( &o )); o->print(); }
 
> Does this program violate any rule of C++?
 
I am pretty sure it does. The program takes the address of o before it is
constructed (this is OK in principle), and then goes on to assign
something to this location, still before the o object has been
constructed - this is definitely UB with a non-trivial class like
std::unique_ptr.
 
From the standard (12.7):
 
"For an object with a non-trivial constructor, referring to any non-
static member or base class of the object before the constructor begins
execution results in undefined behavior."
 
The crucial point here is that std::unique_ptr has a non-trivial
constructor. Note that in C the answer may be different because in C
there is no such thing as a non-trivial constructor.
 
Cheers
Paavo
ram@zedat.fu-berlin.de (Stefan Ram): Feb 19 04:04AM

> o->print();
> o = std::unique_ptr<c>(new c( 2 )); /* overwrite */
> o->print(); }
 
Actually, I wanted to observe how C++ interprets an example
someone posted into the C newsgroup recently. Here is my
attempt of a translation into C++:
 
#include <iostream>
#include <ostream>
#include <memory>
 
struct c /* this struct is as above (as before) */
{ int v;
c( int const x ): v( x )
{ ::std::cout << "constructor of instance #" << v << ".\n"; }
~c(){ ::std::cout << "destructor of instance #" << v << ".\n"; }
void print(){ ::std::cout << "I am instance #" << v << ".\n"; }};
 
::std::unique_ptr< c >f( ::std::unique_ptr< c >* p )
{ *p = ::std::make_unique< c >( 2 ); /* <- sequence point! (semicolon) */
return ::std::make_unique< c >( 1 ); }
 
int main()
{ ::std::unique_ptr< c >o( f( &o )); o->print(); }
 
Does this program violate any rule of C++?
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: