Preventing assigning pointer to variable from temporary which out ofscope

H

hingwah

Consider the following code::

void log_function(const char *str); // 3rd party logging function
which don't use const std::string & as input
class Test
{
public:
std::string getDisplayString();
};
int main()
{
Test obj;
const char * str = obj.getDisplayString().c_str();
log_function(str);;
return 0;
}

This yield undefined behaviour since the string object return from
getDisplayString() go out of scope in which the pointer stored in
variable str is no longer valid.

where as:
int main()
{
Test obj;
log_function(obj.getDisplayString().c_str());
return 0;
}

will work fine since the string return from obj.getDisplayString()
remain exist until log_function return;

sometimes this kind of bug is difficult to catch, and may just work
right fine until your application is in production.

Is there any tricks or any feature in C++0X to make this a compile
error catch by the compiler, e.g. some syntax to indicate return value
from the method invoke by temperatory cannot be assigned to a
variable. (though we still can't prevent log_function to store its
temporary pointer, but that should be easier to catch)

Thanks
 
J

James Kanze

Consider the following code::
void log_function(const char *str); // 3rd party logging function
which don't use const std::string & as input
class Test
{
public:
std::string getDisplayString();};
int main()
{
Test obj;
const char * str = obj.getDisplayString().c_str();
log_function(str);;
return 0;
}
This yield undefined behaviour since the string object return
from getDisplayString() go out of scope in which the pointer
stored in variable str is no longer valid.
where as:
int main()
{
Test obj;
log_function(obj.getDisplayString().c_str());
return 0;
}
will work fine since the string return from obj.getDisplayString()
remain exist until log_function return;
sometimes this kind of bug is difficult to catch, and may just work
right fine until your application is in production.
Is there any tricks or any feature in C++0X to make this a
compile error catch by the compiler, e.g. some syntax to
indicate return value from the method invoke by temperatory
cannot be assigned to a variable. (though we still can't
prevent log_function to store its temporary pointer, but that
should be easier to catch)

Not really. In the case of std::string, the name was chosen
intentionally to make it easy to search for, which is really
what I'd suggest doing. Wrap your log_function in a function
which takes an std::string, and the uses of c_str should be very
rare in your code. Rare enough that you can verify each one
manually.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top