Question about passing STL strings by reference vs. value

M

Mike Cain

Hi - I am looking for the most efficient way to pass a STL string from one
function to another (using MS VS 7.0 ATL if that matters) and have a few
questions abuot the principles at work here.

Typically I have a helper function that does common things like replace all
occurances of a STL string in a given STL string. These strings can be
quite long in lenght, and therefore I do not want to pass the string by
VALUE because of all the memory allocations etc.

So this is how I currently and pass strings around:

APPROACH 1:

string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";

// replace all occurances of foo with bar
MyUtil::replaceAll(&strMyStr, &valToFind, &strReplaceWith);

void MyUtil::replaceAll(string* strMyStr, string* valToFind, string*
strReplaceWith)
{
// reference string values like this: *strMyStr
// call string methods like this: strMyStr->length();
// do replacement, replacing *strMyStr directly with modified
... code goes here, nothing returned
}

APPROACH 2:

string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";

// replace all occurances of foo with bar
MyUtil::replaceAll(strMyStr, valToFind, strReplaceWith);

void MyUtil::replaceAll(string& strMyStr, string& valToFind, string&
strReplaceWith)
{
// reference string values like this: strMyStr (no preceeding * needed
as in approach 1)
// call string methods like this: strMyStr.length();
// do replacement, replacing strMyStr directly with modified
... code goes here, nothing returned
}

Can someone please let me know if approach 1 and 2 are identical in terms of
performance and function? Does approach 2 pass by REFERENCE? If so I much
prefer it over approach 1 because of its simplier and cleaner notiation of
just refering to the var without * and ->.

The only downside with either of these techniques is that it makes for
awkward programming syntax because the target variable must be defined ahead
of time. For instance you have to do this with approach 1 or 2:
string myTargetToHoldVarSetInFunction;
somefunction(&myTargetToHoldVarSetInFunction, &someStrParam1,
&someStrParam2);

Whereas I prefer this:
string myTargetToHoldVarSetInFunction = somefunction(&someStrParam1,
&someStrParam2);

Am I correct, however, with the approach used in that last line of code the
value from the function must be returned by VALUE and therefore a large
string has to be recreated as the function exists and returns a value to the
caller? Any way to use that notation and return by REFERENCE?

Thanks!
 
S

Stuart Golodetz

The only downside with either of these techniques is that it makes for
awkward programming syntax because the target variable must be defined
ahead of time. For instance you have to do this with approach 1 or 2:
string myTargetToHoldVarSetInFunction;
somefunction(&myTargetToHoldVarSetInFunction, &someStrParam1,
&someStrParam2);

Whereas I prefer this:
string myTargetToHoldVarSetInFunction = somefunction(&someStrParam1,
&someStrParam2);

Am I correct, however, with the approach used in that last line of code
the value from the function must be returned by VALUE and therefore a
large string has to be recreated as the function exists and returns a
value to the caller? Any way to use that notation and return by
REFERENCE?

Thanks!

As far as this bit goes, I think it depends on your compiler. Using NRVO
(named return value optimization), I think the compiler's allowed (but not
required) to construct the string directly into the object in the caller
function, i.e. myTarget... in the above.

Trying to find a way to return by reference with things like this is usually
not the right way forward. Local variables no longer exist when the function
exits (so having a reference to them is not a good thing). Trying to keep a
pool of static objects (or similar approaches) fails when you run out of
static objects (i.e. when you call the function one too many times). Other
approaches have similar issues. It's just not a good idea; pass things in by
reference if necessary (i.e. if profiling reveals it to be too slow) but
don't go for anything overly "cunning", which is often a euphemism for
"doesn't always work".

HTH,
Stu
 
D

Daniel T.

"Mike Cain said:
Hi - I am looking for the most efficient way to pass a STL string from one
function to another (using MS VS 7.0 ATL if that matters) and have a few
questions abuot the principles at work here.

Typically I have a helper function that does common things like replace all
occurances of a STL string in a given STL string. These strings can be
quite long in lenght, and therefore I do not want to pass the string by
VALUE because of all the memory allocations etc.

So this is how I currently and pass strings around:

APPROACH 1:

string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";

// replace all occurances of foo with bar
MyUtil::replaceAll(&strMyStr, &valToFind, &strReplaceWith);

void MyUtil::replaceAll(string* strMyStr, string* valToFind, string*
strReplaceWith)
{
// reference string values like this: *strMyStr
// call string methods like this: strMyStr->length();
// do replacement, replacing *strMyStr directly with modified
... code goes here, nothing returned
}

APPROACH 2:

string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";

// replace all occurances of foo with bar
MyUtil::replaceAll(strMyStr, valToFind, strReplaceWith);

void MyUtil::replaceAll(string& strMyStr, string& valToFind, string&
strReplaceWith)
{
// reference string values like this: strMyStr (no preceeding * needed
as in approach 1)
// call string methods like this: strMyStr.length();
// do replacement, replacing strMyStr directly with modified
... code goes here, nothing returned
}

Can someone please let me know if approach 1 and 2 are identical in terms of
performance and function?

Yes they are. If I were you though, I would strive to make the function
const correct:

void MyUtil::replaceAll( string& myStr, const string& valToFind,
const string& replaceWith ) const;
Does approach 2 pass by REFERENCE?
Yes.

The only downside with either of these techniques is that it makes for
awkward programming syntax because the target variable must be defined ahead
of time. For instance you have to do this with approach 1 or 2:
string myTargetToHoldVarSetInFunction;
somefunction(&myTargetToHoldVarSetInFunction, &someStrParam1,
&someStrParam2);

Whereas I prefer this:
string myTargetToHoldVarSetInFunction = somefunction(&someStrParam1,
&someStrParam2);

In the latter choice, how is the function supposed to know what string
to search in?
 
M

Mike Cain

Whereas I prefer this:
Thanks for the quick reply. Glad to hear approach 1 and 2 is identical.
In the latter choice, how is the function supposed to know what string
to search in?

It would be passed as a 3rd param. At a high level I'm trying to figure out
this:

string someResult = somefunction();

Now in somefunction let's say it created a large string. I can't pass this
back by reference because there is no target for that reference right?

Do I understand that the only way to work around this is to use the approach
I described earlier:
string someResult;
somefunction(&someResult);

That gets awkward because then I cannot use simple syntax for other things
like passing this return result to another function that takes a string.
For instance I can't do this: some2ndFunction(somefunction())

I hope my question is making sense but if not please let me know and I will
try to clarify it.

Thanks!
 
D

Daniel T.

Mike Cain said:
At a high level I'm trying to figure out this:

string someResult = somefunction();

Now in somefunction let's say it created a large string. I can't
pass this back by reference because there is no target for that
reference right?

Correct, but who's to say you need to. RVO may make this a non-issue.
Do I understand that the only way to work around this is to use the
approach I described earlier:

string someResult;
somefunction(&someResult);

Or you can use references of course.
That gets awkward because then I cannot use simple syntax for other
things like passing this return result to another function that
takes a string. For instance I can't do this:
some2ndFunction(somefunction())

You can use the iostream trick:

string& someFunction( string& s ) {
// do whatever to 's' then
return s;
}

Now you could:

string result;
some2ndFunction( someFunction( result ) );
I hope my question is making sense but if not please let me know and
I will try to clarify it.

Your making sense all right. But you are likely worrying about something
that simply doesn't matter. Just write the function in the most natural
way:

string result = someFunction();

and let the compiler do it's thing. If it turns out to be too slow, then
you can look into changing someFunction as I describe above.
 
B

BobR

Mike Cain wrote in message ...
It would be passed as a 3rd param. At a high level I'm trying to figure out
this:

string someResult = somefunction();

Now in somefunction let's say it created a large string. I can't pass this
back by reference because there is no target for that reference right?

Do I understand that the only way to work around this is to use the approach
I described earlier:
string someResult;
somefunction(&someResult);

That gets awkward because then I cannot use simple syntax for other things
like passing this return result to another function that takes a string.
For instance I can't do this: some2ndFunction(somefunction())

I hope my question is making sense but if not please let me know and I will
try to clarify it.
Thanks!

????

// include <string>, <iostream>, <ostream>

void SetString(std::string &text){
text = "abcdefgh";
text += ".";
text.at(0) = 'A';
return;
}
std::string SetString2(std::string &text){
text = "abcdefgh";
text += ".";
text.at(0) = 'A';
return text;
}

std::string SetString3(std::string &txt, std::string const &txt2, std::string
const &txt3){
txt = "abcdefgh";
txt += txt2 + txt3;
txt.at(0) = 'A';
return txt;
} // SetString3(string &, string const &, string const &)


// -- put in main() or other func --
std::string SetThis( "" ); // empty string
std::cout<<"before SetString="<<SetThis<<std::endl;
SetString( SetThis );
std::cout<<"after SetString="<<SetThis<<std::endl;
// -output-
// before SetString=
// after SetString=abcdefgh

SetString2( SetThis );
std::cout<<"after SetString2="<< SetThis <<std::endl;
std::cout<<"SetString2="<< SetString2( SetThis ) <<std::endl;
// -output-
// after SetString2=Abcdefgh.
// SetString2=Abcdefgh.

SetThis = "";
std::string S2(" Hello there!");
std::string S3(" How are you today?");
std::string Final = SetString3( SetThis, S2, S3);

std::cout << "SetThis =" << SetThis << std::endl;
// notice that the original string has been modified! (see below)

std::cout << "Final string is =" << Final << std::endl;
// ...and the new string has been assigned to.

SetThis = "";
std::cout << "SetString3( SetThis, S2, S3); =" << SetString3( SetThis, S2,
S3) << std::endl;
// ...and SetString3 will work as an arg to another func. accepting a string.

std::cout << "SetThis.max_size() =" << SetThis.max_size() << std::endl;

// -output-
// SetThis =Abcdefgh Hello there! How are you today?
// Final string is =Abcdefgh Hello there! How are you today?
// SetString3( SetThis, S2, S3); =Abcdefgh Hello there! How are you today?
// SetThis.max_size() =1073741820 // [Intel, win98, MinGW]


If that don't 'splain it, tell us *exactly* what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top