更透彻的理解还需要看stl得具体实现
#include <assert.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
string a = "0123456789abcdefhgjhgghjgjhghghjghghghghjgjhjghghj" ;
vector<int> A;
A.push_back(1);
A.push_back(2);
string b = "ab12345678901234567890";
vector<string> B;
B.push_back(b);
B.push_back(a);
string c = "jkdsfjkhdfjajjuuwerio9898918273818797378218371828387288" ;
B.push_back(c);
return 0;
}
(gdb) p a
$2 = {
<std::__1::__basic_string_common<true>> = {<No data fields>},
members of std::__1::basic_string<char>:
__r_ = {
<std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>> = {
<std::__1::allocator<char>> = {<No data fields>},
members of std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>:
__first_ = {
{
__l = {
__cap_ = 65,
__size_ = 50,
__data_ = 0x100300990 "0123456789abcdefhgjhgghjgjhghghjghghghghjgjhjghghj"
},
__s = {
{
__size_ = 65 'A',
__lx = 65 'A'
},
__data_ = "\000\000\000\000\000\000\000\062\000\000\000\000\000\000\000\220\t0\000\001\000\000"
},
__r = {
__words = {65, 50, 4298115472}
}
}
}
}, <No data fields>},
static npos = 18446744073709551615
}
打印字符串a的信息,原始数据是_r._first.__r__words[3]中,前8个字节为cap,中间八个字节为size,最后八个字节是char指针;若字符串长度小于等于22,则第一个字节是size*2,之后字节是字符串+’\0’;
(gdb) x/24c a.__r_.__first_ .__r.__words
0x7fff5fbff690: 65 'A' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x7fff5fbff698: 50 '2' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x7fff5fbff6a0: -112 '\220' 9 '\t' 48 '0' 0 '\000' 1 '\001' 0 '\000' 0 '\000' 0 '\000'
以字符形式打印word信息
x/xg 0x7fff5fbff6a0
0x7fff5fbff6a0: 0x0000000100300990
以十六进制形式打印char指针,g选项代表以8歌字节为单位进行打印
(gdb) x/s 0x0000000100300990
0x100300990: "0123456789abcdefhgjhgghjgjhghghjghghghghjgjhjghghj"
打印字符串内容,x指令是gdb中查看内存内容的指令
(gdb) p b
$3 = {
<std::__1::__basic_string_common<true>> = {<No data fields>},
members of std::__1::basic_string<char>:
__r_ = {
<std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>> = {
<std::__1::allocator<char>> = {<No data fields>},
members of std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>:
__first_ = {
{
__l = {
__cap_ = 3833745473469047084,
__size_ = 3689065127958034230,
__data_ = 0x30393837363534 <error: Cannot access memory at address 0x30393837363534>
},
__s = {
{
__size_ = 44 ',',
__lx = 44 ','
},
__data_ = "ab12345678901234567890"
},
__r = {
__words = {3833745473469047084, 3689065127958034230, 13573712489362740}
}
}
}
}, <No data fields>},
static npos = 18446744073709551615
}
打印字符串b的信息,b的长度为22,可以直接存储在内存中,不需要char指针
(gdb) x/24c b.__r_ .__first_ .__r.__words
0x7fff5fbff648: 44 ',' 97 'a' 98 'b' 49 '1' 50 '2' 51 '3' 52 '4' 53 '5'
0x7fff5fbff650: 54 '6' 55 '7' 56 '8' 57 '9' 48 '0' 49 '1' 50 '2' 51 '3'
0x7fff5fbff658: 52 '4' 53 '5' 54 '6' 55 '7' 56 '8' 57 '9' 48 '0' 0 '\000'
打印内存中得内容,第一个字符表示size,后面内容是b字符串
(gdb) p B
$4 = {
<std::__1::__vector_base<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >> = {
<std::__1::__vector_base_common<true>> = {<No data fields>},
members of std::__1::__vector_base<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >:
__begin_ = 0x100101db0,
__end_ = 0x100101df8,
__end_cap_ = {
<std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char>*, std::__1::allocator<std::__1::basic_string<char> >, 2>> = {
<std::__1::allocator<std::__1::basic_string<char> >> = {<No data fields>},
members of std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char>*, std::__1::allocator<std::__1::basic_string<char> >, 2>:
__first_ = 0x100101e10
}, <No data fields>}
}, <No data fields>}
打印vector的信息,_begin是容器开始的指针
(gdb) x/250c B.__begin_
0x100101db0(begin): 44 ',' 97 'a' 98 'b' 49 '1' 50 '2' 51 '3' 52 '4' 53 '5'
0x100101db8: 54 '6' 55 '7' 56 '8' 57 '9' 48 '0' 49 '1' 50 '2' 51 '3'
0x100101dc0: 52 '4' 53 '5' 54 '6' 55 '7' 56 '8' 57 '9' 48 '0' 0 '\000'
以上为字符串a
0x100101dc8: 65 'A' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101dd0: 50 '2' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101dd8: 80 'P' 30 '\036' 16 '\020' 0 '\000' 1 '\001' 0 '\000' 0 '\000' 0 '\000'
以上为字符串b,最后一项是char*
0x100101de0: 65 'A' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101de8: 55 '7' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101df0: 16 '\020' 30 '\036' 16 '\020' 0 '\000' 1 '\001' 0 '\000' 0 '\000' 0 '\000'
以上为字符串c,最后一项是char*
0x100101df8(end): 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101e00: 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101e08: 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000’
0x100101e10(end_cap): 106 'j' 107 'k' 100 'd' 115 's' 102 'f' 106 'j' 107 'k' 104 'h'
0x100101e18: 100 'd' 102 'f' 106 'j' 97 'a' 106 'j' 106 'j' 117 'u' 117 'u'
0x100101e20: 119 'w' 101 'e' 114 'r' 105 'i' 111 'o' 57 '9' 56 '8' 57 '9'
0x100101e28: 56 '8' 57 '9' 49 '1' 56 '8' 50 '2' 55 '7' 51 '3' 56 '8'
0x100101e30: 49 '1' 56 '8' 55 '7' 57 '9' 55 '7' 51 '3' 55 '7' 56 '8'
0x100101e38: 50 '2' 49 '1' 56 '8' 51 '3' 55 '7' 49 '1' 56 '8' 50 '2'
0x100101e40: 56 '8' 51 '3' 56 '8' 55 '7' 50 '2' 56 '8' 56 '8' 0 '\000'
0x100101e48: 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x100101e50: 48 '0' 49 '1' 50 '2' 51 '3' 52 '4' 53 '5' 54 '6' 55 '7'
0x100101e58: 56 '8' 57 '9' 97 'a' 98 'b' 99 'c' 100 'd' 101 'e' 102 'f'
0x100101e60: 104 'h' 103 'g' 106 'j' 104 'h' 103 'g' 103 'g' 104 'h' 106 'j'
0x100101e68: 103 'g' 106 'j' 104 'h' 103 'g' 104 'h' 103 'g' 104 'h' 106 'j'
0x100101e70: 103 'g' 104 'h' 103 'g' 104 'h' 103 'g' 104 'h' 103 'g' 104 'h'
0x100101e78: 106 'j' 103 'g' 106 'j' 104 'h' 106 'j' 103 'g' 104 'h' 103 'g'
0x100101e80: 104 'h' 106 'j' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
最后是string中char*指针指向的内容。