Am Fri, 2. Mar 2012, 23:27:33 +0800 schrieb Daniel Hartwig: > On 2 March 2012 22:26, Jens Seidel <[email protected]> wrote: > > + return theme_config->FindVector((themeroot+Name).c_str()); > > That is certainly an issue to be aware of. In this case it is safe to > call FindVector like this as the temporary string will remain allocated > until the function returns. > > I have been using such temporaries extensively in recent changes. I > did have the same concern as yourself when I started so I checked this > out with a small test program first and found some confirmation[1] > online (though did not manage to locate anything in C++ reference). > > [1] > http://stackoverflow.com/questions/7581680/pass-c-str-return-value-of-temporary-object-to-printf
Such tests are are useful but should be taken with a grain of salt, since they can succeed when you actually have undefined behaviour (e.g. use after free). According to the standard[1] (§12.2 paragraph 3) you should be OK provided FindVector() doesn't store a reference or pointer into its argument string somewhere (e.g. in its return value). Guessing from the name of the function it just returns are reference into the container *theme_config, so there shouldn't be a problem. The basic rule is that the lifetime of a temporary extends until the execution of the full-expression ends. In this case this means until the end of "theme_config->FindVector((themeroot+Name).c_str())". Of course, every basic rule has exceptions (see §12.2 paragraphs 4 and 5), but I dont think they apply here. Bye, Jö. [1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf Latest freely accessible draft... -- Jorrit (Jö) Fahlke, Interdisciplinary Center for Scientific Computing, Heidelberg University, Im Neuenheimer Feld 368, D-69120 Heidelberg Tel: +49 6221 54 8890 Fax: +49 6221 54 8884 If you receive something that says "Send this to everyone you know," pretend you don't know me.
signature.asc
Description: Digital signature
_______________________________________________ Aptitude-devel mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/aptitude-devel

