新闻  |   论坛  |   博客  |   在线研讨会
C++ string类的c_str()方法小结
电子禅石 | 2024-08-08 18:18:11    阅读:17030   发布文章

最近研究了一下基于c++的大数乘法算法, 碰到了string类对象与c风格字符串转换问题,有一个问题令我印象深刻,现在没有找到具体原因,记录下来。代码环境是Linux ubuntu,编译器是g++


觉得写得比较好的博客罗列如下:


【转】https://www.cnblogs.com/lifexy/p/8642163.html


1.c_str()


c_str()方法返回一个const char* 类型的指针变量, 该指针变量指向一个字符数组, 字符数组的元素个数是string::length() + 1,最后一个元素为 '\0'


以下为官方说法:


const charT* c_str() const;

Returns: A pointer to the initial element of an array of length size() + 1 whose first size() elements equal the corresponding elements of the string controlled by *this and whose last element is a null character specified by charT().

Requires: The program shall not alter any of the values stored in the array. Nor shall the program treat the returned value as a valid pointer value after any subsequent call to a non-const member function of the class basic_string that designates the same object as this.

--------------------- 



2.在实验中, 不论我在堆上(new)或者在栈上创建一个string类实例, 调用c_str()方法后发现字符数组并没有随着实例的变化而变化,即使实例销毁(delete或者自己析构),字符数组也没有变化;但是当我改变string的原始内容时, c_str()方法就会有所变化, 记录如下:


当string实例的初始值是"string in stack"时


root@ubuntu:/lianxi/lianxi_c++/string# g++ test_c_str.cpp 

root@ubuntu:/lianxi/lianxi_c++/string# ./a.out

=====TEST IN STACK=====

aStr.length() = 15

q[0] = 115

q[1] = 116

q[2] = 114

q[3] = 105

q[4] = 110

q[5] = 103

q[6] = 32

q[7] = 105

q[8] = 110

q[9] = 32

q[10] = 115

q[11] = 116

q[12] = 97

q[13] = 99

q[14] = 107

q[15] = 0

&aStr = 0xbf8df7f0, q = 0x93e4014

aStr1:string in stack

q1:string in stack

aStr2:change in stack

q2:change in stack

q3:change in stack

=====TEST OVER=====

root@ubuntu:/lianxi/lianxi_c++/string#

  1 #include <iostream>

  2 #include <string>

  3 #include <string.h>

  4 #include <strings.h>

  5 #include <stdio.h>

  6 

  7 using namespace std;

  8 

  9 /*we use printf in this test instead of cout*/

 10 int main(void)

 11 {

 12     printf("=====TEST IN STACK=====\n");

 13     const char* q = NULL;

 14     /*code block*/

 15     {

 16         string aStr("string in stack");

 17         printf("aStr.length() = %d\n", aStr.length());//15

 18         

 19         q = aStr.c_str();

 20         int j = 0;

 21         for(j = 0; j < aStr.length() + 1; j++)

 22         {

 23             printf("q[%d] = %d\n", j, q[j]);

 24         }   

 25         

 26         printf("&aStr = %p, q = %p\n", &aStr, q);//address are different

 27         

 28         cout << "aStr1:" << aStr << endl;//string in stack

 29         printf("q1:%s\n", q);            //string in stack

 30         

 31         aStr = "change in stack";

 32         cout << "aStr2:" << aStr << endl;//change in stack

 33         printf("q2:%s\n", q);            //change in stack

 34     }   

 35     

 36     printf("q3:%s\n", q);                //change in stack

 37     printf("=====TEST OVER=====\n");

 38     return (0);

 39 }

当string实例的初始值是"abcdefg"时:


root@ubuntu:/lianxi/lianxi_c++/string# g++ test_c_str.cpp 

root@ubuntu:/lianxi/lianxi_c++/string# ./a.out

=====TEST IN STACK=====

aStr.length() = 7

q[0] = 97

q[1] = 98

q[2] = 99

q[3] = 100

q[4] = 101

q[5] = 102

q[6] = 103

q[7] = 0

&aStr = 0xbfbae130, q = 0x9ba4014

aStr1:abcdefg

q1:abcdefg

aStr2:change in stack

q2:abcdefg

q3:abcdefg

=====TEST OVER=====

root@ubuntu:/lianxi/lianxi_c++/string# 

  1 #include <iostream>

  2 #include <string>

  3 #include <string.h>

  4 #include <strings.h>

  5 #include <stdio.h>

  6 

  7 using namespace std;

  8 

  9 /*we use printf in this test instead of cout*/

 10 int main(void)

 11 {

 12     printf("=====TEST IN STACK=====\n");

 13     const char* q = NULL;

 14     /*code block*/

 15     {

 16         //string aStr("string in stack");

 17         string aStr("abcdefg");

 18         printf("aStr.length() = %d\n", aStr.length());//15//7

 19         

 20         q = aStr.c_str();

 21         int j = 0; 

 22         for(j = 0; j < aStr.length() + 1; j++)

 23         {   

 24             printf("q[%d] = %d\n", j, q[j]);

 25         }

 26         

 27         printf("&aStr = %p, q = %p\n", &aStr, q);//address are different

 28         

 29         cout << "aStr1:" << aStr << endl;//string in stack//abcdefg

 30         printf("q1:%s\n", q);            //string in stack//abcdefg

 31         

 32         aStr = "change in stack";

 33         cout << "aStr2:" << aStr << endl;//change in stack

 34         printf("q2:%s\n", q);            //change in stack//abcdefg

 35     }

 36     

 37     printf("q3:%s\n", q);                //change in stack//abcdefg

 38     printf("=====TEST OVER=====\n");

 39     return (0);

 40 }

【注意】:这种机制目前我并不明确, 估计是编译器的某种行为, 但是可以明确看到c_str()方法返回的数组里面有length+1个元素, 且最后一个元素为'\0'

————————————————


原文链接:https://blog.csdn.net/liaojunwu/article/details/85269931


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客