C++ string類的c_str()方法小結
最近研究了一下基于c++的大數乘法算法, 碰到了string類對象與c風格字符串轉換問題,有一個問題令我印象深刻,現在沒有找到具體原因,記錄下來。代碼環(huán)境是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)或者在棧上創(chuàng)建一個string類實例, 調用c_str()方法后發(fā)現字符數組并沒有隨著實例的變化而變化,即使實例銷毀(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()方法返回的數組里面有l(wèi)ength+1個元素, 且最后一個元素為'\0'
————————————————
原文鏈接:https://blog.csdn.net/liaojunwu/article/details/85269931
*博客內容為網友個人發(fā)布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。