473,386 Members | 2,129 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,386 software developers and data experts.

Preventing assigning pointer to variable from temporary which out ofscope

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
Jun 27 '08 #1
1 1813
On Apr 19, 11:22 pm, hing...@gmail.com wrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #2

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

Similar topics

7
by: NS Develop | last post by:
What's wrong with the following: void SomeClass::someMethod(void) { A *ptrA = NULL; ptrA = &A(); .... }
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
19
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
11
by: Hipo | last post by:
Hi. I have the following code: unsigned char temporary; unsigned __int64 *reader = (unsigned __int64*)&temporary+20; std::cout << *reader-- << std::endl; std::cout << *reader-- << ...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
31
by: leonm54 | last post by:
I remember that this is a bad practice, but can not find a definitive resource that states it is bad and why. Can anyone help?
3
by: Donos | last post by:
Hello I have the following program, void getValue(unsigned char* pVal) { pVal = allValues(); } unsigned char* allValues()
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.