C++ string類的c_str()方法小結(jié)
最近研究了一下基于c++的大數(shù)乘法算法, 碰到了string類對(duì)象與c風(fēng)格字符串轉(zhuǎn)換問(wèn)題,有一個(gè)問(wèn)題令我印象深刻,現(xiàn)在沒(méi)有找到具體原因,記錄下來(lái)。代碼環(huán)境是Linux ubuntu,編譯器是g++
覺(jué)得寫(xiě)得比較好的博客羅列如下:
【轉(zhuǎn)】https://www.cnblogs.com/lifexy/p/8642163.html
1.c_str()
c_str()方法返回一個(gè)const char* 類型的指針變量, 該指針變量指向一個(gè)字符數(shù)組, 字符數(shù)組的元素個(gè)數(shù)是string::length() + 1,最后一個(gè)元素為 '\0'
以下為官方說(shuō)法:
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.在實(shí)驗(yàn)中, 不論我在堆上(new)或者在棧上創(chuàng)建一個(gè)string類實(shí)例, 調(diào)用c_str()方法后發(fā)現(xiàn)字符數(shù)組并沒(méi)有隨著實(shí)例的變化而變化,即使實(shí)例銷毀(delete或者自己析構(gòu)),字符數(shù)組也沒(méi)有變化;但是當(dāng)我改變string的原始內(nèi)容時(shí), c_str()方法就會(huì)有所變化, 記錄如下:
當(dāng)string實(shí)例的初始值是"string in stack"時(shí)
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 }
當(dāng)string實(shí)例的初始值是"abcdefg"時(shí):
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 }
【注意】:這種機(jī)制目前我并不明確, 估計(jì)是編譯器的某種行為, 但是可以明確看到c_str()方法返回的數(shù)組里面有l(wèi)ength+1個(gè)元素, 且最后一個(gè)元素為'\0'
————————————————
原文鏈接:https://blog.csdn.net/liaojunwu/article/details/85269931
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。