Print

Print


The c_str() of a temporary std::string object is not valid after the
lifetime of the object expires. Consider:

$ cat temp.cpp
int main() {
  const char* c1 = std::string("ABC").c_str();
  const char* c2 = std::string("DEF").c_str();
  std::cout << c1 << std::endl;
  std::cout << c2 << std::endl;
}

$ g++ -O2 -o temp temp.cpp
$ ./temp
DEF
DEF

The memory used by the first temporary std::string is reused by the
second one, since the lifetime of the first has expired. So when the
saved c_str() of the first string is written out, it writes the second
string.


You can view, comment on, or merge this pull request online at:

  https://github.com/xrootd/xrootd/pull/1368

Commit Summary

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/xrootd/xrootd/pull/1368", "url": "https://github.com/xrootd/xrootd/pull/1368", "name": "View Pull Request" }, "description": "View this Pull Request on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

Use REPLY-ALL to reply to list

To unsubscribe from the XROOTD-DEV list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=XROOTD-DEV&A=1