c++primer中文版201页说函数不能返回局部对象的指针或引用。
const string &manip()
{
string ret;
if(!ret.empty())
return ret; //错误返回局部对象的引用
else
return "empty"; //错误 “empty”是一个局部临时量
}
我在essential c++看到了这样的代码,函数可以返回局部变量的char 指针 和const char *,这是为什么呢?(在win7 + vs2010下编译通过并可以运行打印"num_sequence")
#include <iostream>
#include <string>
using namespace std;
class num_sequence{
public:
virtual const char *what_am_i( ) const// 返回值char * 和const char*可以编译通过
{
return "num_sequence";
}
};
int main()
{
num_sequence p;
std::cout<<p.what_am_i();
}
把函数返回值有const char改为char 也可以编译通过,但不能通过char指针修改字符串的内容。
virtual char *what_am_i( ) const//把函数返回值有const char*改为char *也可以编译通过
{
return "num_sequence";
}
问题2我好像了解了,类似于char* p= “safdsaf”;也是不能通过指针修改rodata里的内容的。
The above two are temporary variables returned (
return "empty"
Construct a temporary string first, and then return), which will be destructed at the end of the function call. So it is unsafe. The compilation can pass, but the returned result should not be used because it has been destructed.What is returned above is a pointer pointing to the string "num_sequence". This string is in the global static area and always exists. It is not temporary, so there is no problem.
For question 2
In fact, no matter whether it is
char*
orconst char*
, you cannot modify it, because it points to a constant string. Forcibly modifying will cause an error when running.The answer above is relatively clear. You need to understand the memory layout of the process.
It is generally believed that the memory layout of a process includes the following areas:
1) Code area
2) Literal constant area
3) Global area/static area
4) Heap area
5 ) Stack area
can be found at http://blog.csdn.net/duyiwuer2009/article/details/7994091
So, if you look at the example below, as long as the strings referencing the constant area are the same, then their The address value is the same instead of constructing an object.
Output on my computer
And for your question 1,
Because the return type is a constant reference, and the actual value returned is a constant string, a temporary variable with the content "empty" and type string will be constructed first during this process, and then returned to the caller. During the return process, this temporary variable is released. If you continue to access, the access may or may not be successful, but no matter what, the data is already dirty data. This behavior in C++ does not stipulate that it must be a program crash or failure. If it does not crash, it is collectively called undefined behavior. It is determined by the running context whether it crashes or not.