473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Lifespan of anonymous instances

In this snippet:

void f(const char* c) {...use c in a read-only way...}

std::ostringstream oss;
oss << "blah blah";
f(oss.str().c_str());

oss.str() returns a *copy* of the buffer as a std::string. In this case
this copy is anonymous, and the return value of its c_str() method is
passed to f(). Is the anonymous std::string instance guaranteed (by the
standards) to last long enough for f() to safely use its c_str() value?

Aug 30 '05 #1
4 1754
ri***********@yahoo.co.uk wrote:
In this snippet:

void f(const char* c) {...use c in a read-only way...}

std::ostringstream oss;
oss << "blah blah";
f(oss.str().c_str());

oss.str() returns a *copy* of the buffer as a std::string. In this case
this copy is anonymous, and the return value of its c_str() method is
passed to f(). Is the anonymous std::string instance guaranteed (by the
standards) to last long enough for f() to safely use its c_str() value?


Yes.

Only after the call to f() completes will the object passed as a
parameter be destroyed.

Greg

Aug 30 '05 #2
Thanks, but what was worrying me in this case is that it is not the
anonymous instance *itself* that is being passed as a parameter, but
the return value of one of its methods - does your answer still apply
in that case?

Ta
Richard

Aug 30 '05 #3
Thanks, but what was worrying me in this case is that it is not the
anonymous instance *itself* that is being passed as a parameter, but
the return value of one of its methods - does your answer still apply
in that case?

Ta
Richard

Aug 30 '05 #4
ri***********@yahoo.co.uk wrote:
Thanks, but what was worrying me in this case is that it is not the
anonymous instance *itself* that is being passed as a parameter, but
the return value of one of its methods - does your answer still apply
in that case?


Yes.

The result of c_str() is valid until you call the next non-const
memberfunction of the string. The temporary string in your example is
valid until f returns (at which point the string is destroyed and thus
the result of c_str() also becomes invalid)

Gabriel
Aug 30 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
1
by: Mark | last post by:
Hi.. One of our webdev guys is having trouble with IIS set up on his system, particularly with anonymous authentication. On all of his web instances, he can get to non-asp pages but all asp pages...
4
by: Nicholas Beenham | last post by:
Hi all, Is there a way to set the lifespan of a thread. I am trying to start a process that may or may not have an outcome and want to finish it after a certain length of time finished or not....
4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
4
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; When a Page object - the code-behind of an aspx page is created - what is the lifespan of that created object? Is it tied to the session, the request/response, or is it in a poll and reused...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
3
by: David Cox | last post by:
I found some details on a musical festival, made my plans, and only found out by chance that the site was two years out of date. Is there some HTML way of setting a lifespan on a webpage on...
2
by: Tony | last post by:
Hello! Here I have some text from a book I read. It says: "An interesting point to note concerning anonymous methods is that they are effectively local to the code block that contains them, and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.