首页 技术 正文
技术 2022年11月8日
0 收藏 810 点赞 1,672 浏览 4110 个字

——————— 
作者:心中那自由的世界 
来源:CSDN 
原文:https://blog.csdn.net/119365374/article/details/77446786

在编写应用程序时,我们经常要使用到字符串。C++标准库中的<string>和<sstream>为我们操作字符串提供了很多的方便,例如:对象封装、安全和自动的类型转换、直接拼接、不必担心越界等等。但今天我们并不想长篇累牍得去介绍这几个标准库提供的功能,而是分享一下stringstream.str()的一个有趣的现象。我们先来看一个例子:

    #include <string>
#include <sstream>
#include <iostream> using namespace std; int main()
{
stringstream ss("");
stringstream t_ss("abcdefghijklmnopqrstuvwxyz");
string str1(ss.str()); const char* cstr1 = str1.c_str();
const char* cstr2 = ss.str().c_str();
const char* cstr3 = ss.str().c_str();
const char* cstr4 = ss.str().c_str();
const char* t_cstr = t_ss.str().c_str(); cout << "------ The results ----------" << endl
<< "cstr1:\t" << cstr1 << endl
<< "cstr2:\t" << cstr2 << endl
<< "cstr3:\t" << cstr3 << endl
<< "cstr4:\t" << cstr4 << endl
<< "t_cstr:\t" << t_cstr << endl
<< "-----------------------------" << endl; return ;
}

在看这段代码的输出结果之前,先问大家一个问题,这里cstr1、cstr2、cstr3和cstr4 打印出来结果是一样的么?(相信读者心里会想:结果肯定不一样的嘛,否则不用在这里“故弄玄虚”了。哈哈

接下来,我们来看一下这段代码的输出结果:

    ------ The results ----------
cstr1:
cstr2:
cstr3: abcdefghijklmnopqrstuvwxyz
cstr4: abcdefghijklmnopqrstuvwxyz
t_cstr: abcdefghijklmnopqrstuvwxyz
-----------------------------

这里,我们惊奇地发现cstr3和cstr4竟然不是ss所表示的数字字符串,而是t_ss所表示的字母字符串,这也太诡异了吧,但我们相信“真相只有一个”。下面我们通过再加几行代码来看看,为什么会出现这个“诡异”的现象。

   #include <string>
#include <sstream>
#include <iostream> using namespace std; #define PRINT_CSTR(no) printf("cstr" #no " addr:\t%p\n",cstr##no)
#define PRINT_T_CSTR(no) printf("t_cstr" #no " addr:\t%p\n",t_cstr##no) int main()
{
stringstream ss("");
stringstream t_ss("abcdefghijklmnopqrstuvwxyz");
string str1(ss.str()); const char* cstr1 = str1.c_str();
const char* cstr2 = ss.str().c_str();
const char* cstr3 = ss.str().c_str();
const char* cstr4 = ss.str().c_str();
const char* t_cstr = t_ss.str().c_str(); cout << "------ The results ----------" << endl
<< "cstr1:\t" << cstr1 << endl
<< "cstr2:\t" << cstr2 << endl
<< "cstr3:\t" << cstr3 << endl
<< "cstr4:\t" << cstr4 << endl
<< "t_cstr:\t" << t_cstr << endl
<< "-----------------------------" << endl;
printf("\n------ Char pointers ----------\n");
PRINT_CSTR();
PRINT_CSTR();
PRINT_CSTR();
PRINT_CSTR();
PRINT_T_CSTR(); return ;
}

在上述代码中,我们把那几个字符串对应的地址打印出来,其输出结果为:

    ------ The results ----------
cstr1:
cstr2:
cstr3: abcdefghijklmnopqrstuvwxyz
cstr4: abcdefghijklmnopqrstuvwxyz
t_cstr: abcdefghijklmnopqrstuvwxyz
----------------------------- ------ Char pointers ----------
cstr1 addr: 0x100200e4
cstr2 addr: 0x10020134
cstr3 addr: 0x10020014
cstr4 addr: 0x10020014
t_cstr addr: 0x10020014

从上面的输出,我们发现cstr3和cstr4字串符的地址跟t_cstr是一样,因此,cstr3、cstr4和t_cstr的打印结果是一样的。按照我们通常的理解,当第17-19行调用ss.str()时,将会产生三个string对象,其对应的字符串也将会是不同的地址。

而打印的结果告诉我们,真实情况不是这样的。其实,streamstring在调用str()时,会返回临时的string对象。而因为是临时的对象,所以它在整个表达式结束后将会被析构。由于紧接着调用的c_str()函数将得到的是这些临时string对象对应的C string,而它们在这个表达式结束后是不被引用的,进而这块内存将被回收而可能被别的内容所覆盖,因此我们将无法得到我们想要的结果。虽然有些情况下,这块内存并没有被别的内容所覆盖,于是我们仍然能够读到我们期望的字符串,(这点在这个例子中,可以通过将第20行删除来体现)。但我们要强调的是,这种行为的正确性将是不被保证的。

通过上述分析,我们将代码修改如下:

   #include <string>
#include <sstream>
#include <iostream> using namespace std; #define PRINT_CSTR(no) printf("cstr" #no " addr:\t%p\n",cstr##no)
#define PRINT_T_CSTR(no) printf("t_cstr" #no " addr:\t%p\n",t_cstr##no) int main()
{
stringstream ss("");
stringstream t_ss("abcdefghijklmnopqrstuvwxyz");
string str1(ss.str()); const char* cstr1 = str1.c_str();
const string& str2 = ss.str();
const char* cstr2 = str2.c_str();
const string& str3 = ss.str();
const char* cstr3 = str3.c_str();
const string& str4 = ss.str();
const char* cstr4 = str4.c_str();
const char* t_cstr = t_ss.str().c_str(); cout << "------ The results ----------" << endl
<< "cstr1:\t" << cstr1 << endl
<< "cstr2:\t" << cstr2 << endl
<< "cstr3:\t" << cstr3 << endl
<< "cstr4:\t" << cstr4 << endl
<< "t_cstr:\t" << t_cstr << endl
<< "-----------------------------" << endl;
printf("\n------ Char pointers ----------\n");
PRINT_CSTR();
PRINT_CSTR();
PRINT_CSTR();
PRINT_CSTR();
PRINT_T_CSTR(); return ;
}

现在我们将获得我们所期望的输出结果了:

    ------ The results ----------
cstr1:
cstr2:
cstr3:
cstr4:
t_cstr: abcdefghijklmnopqrstuvwxyz
----------------------------- ------ Char pointers ----------
cstr1 addr: 0x100200e4
cstr2 addr: 0x10020134
cstr3 addr: 0x10020184
cstr4 addr: 0x100201d4
t_cstr addr: 0x10020014

现在我们知道stringstream.str()方法将返回一个临时的string对象,而它的生命周期将在本表达式结束后完结。当我们需要对这个string对象进行进一步操作(例如获得对应的C string)时,我们需要注意这个可能会导致非预期结果的“陷阱”。:)

最后,我们想强调一下:由于临时对象占用内存空间被重新使用的不确定性,这个陷阱不一定会明显暴露出来。但不暴露出来不代表行为的正确性,为了避免“诡异”问题的发生,请尽量采用能保证正确的写法。

正确的写法应该是string str = stringstream.str();

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287