using namespace std;
string getString();
void displayString(const char * p);
string getString()
{
string sVal = "TEST";
return sVal;
}
void displayString(const char * p)
{
printf("*** %s ***\n", p);
}
int _tmain()
{
// This seems to work - is this guaranteed?
displayString(getString().c_str());
// This doesn't work - the string that is returned by getString() goes out of scope immediatiely after this line
const char * p2 = getString().c_str();
displayString(p2);
}
C++ scope of function return values
Page 1 of 18 Replies - 771 Views - Last Post: 04 March 2010 - 11:44 PM
#1
C++ scope of function return values
Posted 04 March 2010 - 08:04 PM
Replies To: C++ scope of function return values
#2
Re: C++ scope of function return values
Posted 04 March 2010 - 08:26 PM
this c++ code seems to be working with XCode...
you might rename the main to _tmain as your original post had it.
#include <iostream>
using namespace std;
string getString();
void displayString(string p);
string getString()
{
string sVal = "TEST";
return sVal;
}
void displayString(string p)
{
cout <<"***** "<< p << " *****"<<endl;
}
int main()
{
displayString(getString().c_str());
string p2 = getString().c_str();
displayString(p2);
return 0;
}
as for the scope of return values,
you need to assign the return value to some variable, otherwise there is no variable pointing/referencing to the value returned by the function.
so for example,
assume that string myString() exists...
myString(); // will return a string but you need to store it into variable to use it.....
so sumthing like...
string myStr = myString(); cout << myStr; //you could use it many times... cout << "printing it again.." << myStr;
#3
Re: C++ scope of function return values
Posted 04 March 2010 - 08:53 PM
javabie, on 04 March 2010 - 07:26 PM, said:
this c++ code seems to be working with XCode...
you might rename the main to _tmain as your original post had it.
#include <iostream>
using namespace std;
string getString();
void displayString(string p);
string getString()
{
string sVal = "TEST";
return sVal;
}
void displayString(string p)
{
cout <<"***** "<< p << " *****"<<endl;
}
int main()
{
displayString(getString().c_str());
string p2 = getString().c_str();
displayString(p2);
return 0;
}
as for the scope of return values,
you need to assign the return value to some variable, otherwise there is no variable pointing/referencing to the value returned by the function.
so for example,
assume that string myString() exists...
myString(); // will return a string but you need to store it into variable to use it.....
so sumthing like...
string myStr = myString(); cout << myStr; //you could use it many times... cout << "printing it again.." << myStr;
Thanks, but I already know that the code will work if displayString accepts a string parameter.
The point is that I've get a bunch of legacy functions that accept const char * that I don't want to / can't change.
ie I'm hoping that this is a safe thing to do:
legacyFunction(getString().c_str());
instead of having to do this everywhere:
string sTemp = getString();
legacyFunction(sTemp.c_str());
#4
Re: C++ scope of function return values
Posted 04 March 2010 - 09:47 PM
using namespace std;
string getString();
void displayString(const char * p);
string getString()
{
string sVal = "TEST"; <<<<<< Put the sVal variable on the stack ...
<<<<<< and assign a value "TEST"
return sVal; <<<<<< Set EAX register to contain ...
<<<<<< ... address of sVal (on the stack)
} <<<<<< Return to caller removing sVal ...
<<<<<< from the stack
<<<<<< Effectively sVal is no longer valid
<<<<<< as the stack pointer is above its
<<<<<< address
void displayString(const char * p)
{
printf("*** %s ***\n", p);
}
int _tmain()
{
// This seems to work - is this guaranteed? <<<<<< absolutely NOT guarateed
displayString(getString().c_str());
// This doesn't work - the string that is returned by getString() goes out of scope immediatiely after this line
const char * p2 = getString().c_str();
displayString(p2);
}
Without knowing assembler, this is difficult to explain in a way that really makes sense. If you have any questions please ask.
BTW I have commented the code but you will have to use the horizontal scroll to see them.
This post has been edited by Martyn.Rae: 04 March 2010 - 09:48 PM
#5
Re: C++ scope of function return values
Posted 04 March 2010 - 09:50 PM
To quote cplusplus.com:
Quote
#6
Re: C++ scope of function return values
Posted 04 March 2010 - 09:57 PM
#7
Re: C++ scope of function return values
Posted 04 March 2010 - 11:24 PM
Though I think you bring up a good point that one has to keep in mind that the return value of string::c_str() is tied to the life of the object -- in fact it is tied to the object, if that object leaves scope, or it is modified then the array pointed to by a previous call to c_str() will be invalid.
#8
Re: C++ scope of function return values
Posted 04 March 2010 - 11:28 PM
This post has been edited by Martyn.Rae: 04 March 2010 - 11:30 PM
#9
Re: C++ scope of function return values
Posted 04 March 2010 - 11:44 PM
const char* getStr() {
string a = "whatever";
return a.c_str();
}
The code looks innocent enough but since the string is no long in scope once the function ends the return value is no good. I have written code like this myself and been oh so terribly confused when it was not working. Actually with me it would be more something like this:
const char *ptr;
for(whatever) {
string s = "whatever";
ptr = s.c_str();
}
cout << ptr; //Crash....
This is the kind of thing that will get one in trouble if you don't understand scope! (which apparently I don't from time to time).
|
|

New Topic/Question
Reply




MultiQuote




|