Re: std::string as function parameter
I'm resurrecting this thread to ask one other thing about passing std::string.
What about a function returning std::string?
suppose:
Code:
std::string test(std::string &str) {
std:string s;
s = str;
s.append("This is a string.");
return(s);
}
std::string str1, str2;
str1 = "Data...";
// Does this call the operator= method and copy the returned data?
// Or does it merely memcpy the data structure?
// When does the returned std::string get deleted?
// Is this even safe? Memory leaks?
str2 = test(str1);
// Could I even do this?
str2 = test(string("More data..."));
Is there a safe way to return STL containers from a function, or should I always use an output variable like:
Code:
void test(std::string &ret) {
ret = "This is a string.";
}
Re: std::string as function parameter
Quote:
Originally Posted by spoulson
I'm resurrecting this thread to ask one other thing about passing std::string.
What about a function returning std::string?
// Does this call the operator= method and copy the returned data?
// Or does it merely memcpy the data structure?
The operator = is called. Also, forget that memcpy() exists -- that's the easiest way to explain what happens.
Quote:
// When does the returned std::string get deleted?
When it goes out of scope, just like any other object.
Quote:
// Is this even safe? Memory leaks?
str2 = test(str1);
If it weren't safe, no one would be using std::string, except for toy programs.
The bottom line is that for all the classes in the standard library (and for your own classes), it is imperative that the class behaves just like a built-in type when it comes to assignment, copying, and destruction. If not, we would have the some of the most buggiest programs to maintain and fix.
Quote:
Is there a safe way to return STL containers from a function,
This is not an STL issue. Any class, whether it is an STL class or whether it's one that you wrote, must be properly assignable and copyable. Again, if this is not the case, the class is buggy. If we return an STL object or your own object from a function, it better have correct copy semantics. So the answer to your question is yes, there is absolutely nothing wrong in terms of memory leaks when returning an STL container.
Regards,
Paul McKenzie
Re: std::string as function parameter
Good answer.
So, let me ask, when exactly does a returned type go out of scope? I was afraid that it would go out of scope before getting assigned to the return variable or something like that.
Re: std::string as function parameter
As far as I know, a returned type goes out of scope when the function ends (yes, that simply). But the copying of the returned object into a temporary occurs before the function ends. Then when re-assumming the previous scope the object in the temproary adress is copied into the destination adress (the object being assigned to). And in certain cases where the returned object is constructed while returning, it is considered to be the temporary object and it is worked outside the function scope.
But I'm not an expert :-D, I just debug a lot... :confused: