Whole document tree
7.10.2 Temporaries May Vanish Before You Expect
It is dangerous to use pointers or references to portions of a
temporary object. The compiler may very well delete the object before
you expect it to, leaving a pointer to garbage. The most common place
where this problem crops up is in classes like string classes,
especially ones that define a conversion function to type
For example, a program may use a function
In this situation, it may seem reasonable to save a pointer to the C
string returned by the Code like this may run successfully under some other compilers, particularly obsolete cfront-based compilers that delete temporaries along with normal local variables. However, the GNU C++ behavior is standard-conforming, so if your program depends on late destruction of temporaries it is not portable. The safe way to write such code is to give the temporary a name, which forces it to remain until the end of the scope of the name. For example:
This document was generated by root on January, 30 2002 using texi2html |