std::string的底层实现
std::string的底层实现
std::string的底层到底是如何实现的呢? 其实在std::string的历史中,出现过几种不同的方式。
可以从一个简单的问题来探索,一个std::string对象占据的内存空间有多大,即sizeof(std::string)的值为多大?在不同的编译器(VC++, GCC, Clang++)上去测试,可能会发现其值并不相同;即使是GCC,不同的版本,获取的值也是不同的。
虽然历史上的实现有多种,但基本上有三种方式:
-
Eager Copy(深拷贝)
-
COW(Copy-On-Write 写时复制)
-
SSO(Short String Optimization 短字符串优化)
std::string的底层实现是一个高频考点,虽然目前std::string是根据SSO的思想实现的,但是最好能够掌握其发展过程中的不同设计思想。
深拷贝及string相关运算符重载
首先,最简单的就是深拷贝。无论什么情况,都是采用拷贝字符串内容的方式解决。这种实现方式,在不需要改变字符串内容时,对字符串进行频繁复制,效率比较低下。所以需要对其实现进行优化,之后便出现了下面的COW的实现方式。
//如果 string 的实现直接用深拷贝
string str1("hello, world");
string str2 = str1;
如上,str2保存的字符串内容与str1完全相同,但是根据深拷贝的思想,一定要重新申请空间、复制内容,这样效率较低、开销较大。
自定义String类实现
#include <iostream>
#include <string.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
// 自定义String类
class String{
public:
String()
:_pstr(new char[1]())
{
cout << "String()" << endl;
}
// String s1("hello");
// String s2 = "hello";
String(const char *pstr)
:_pstr(new char[strlen(pstr)+1]())
{
cout << "String(const char *)" << endl;
strcpy(_pstr,pstr);
}
// String s2(s1);
// String s3 = s1;
String(const String &rhs)
:_pstr(new char[strlen(rhs._pstr)+1]())
{
cout << "String(const String &)" << endl;
strcpy(_pstr,rhs._pstr);
}
~String()
{
cout << "~String()" << endl;
if(_pstr)
{
delete [] _pstr;
_pstr = nullptr;
}
}
// String s1("hello");
// String s2;
// s2 = s1;
String& operator=(const String &rhs)
{
cout << "String& operator=(const String &rhs)" << endl;
if(this != &rhs)
{
delete [] _pstr;
_pstr = nullptr;
_pstr = new char[strlen(rhs._pstr) + 1]();
strcpy(_pstr,rhs._pstr);
}
return *this;
}
// s1 = "hello";
String& operator=(const char * pstr)
{
cout << "String& operator=(const char * pstr)" << endl;
String tmp(pstr); //增量开发; 调用拷贝构造String(const char *)
*this = tmp;
return *this;
}
// s1 += s2;
String& operator+=(const String &rhs)
{
cout << "String& operator+=(const String &rhs)" << endl;
String tmp;
if(tmp._pstr)
{
delete [] tmp._pstr;
}
tmp._pstr = new char[strlen(_pstr) + 1]();
strcpy(tmp._pstr,_pstr);
delete [] _pstr;
_pstr = new char[strlen(_pstr) + strlen(rhs._pstr) + 1]();
strcpy(_pstr,tmp._pstr);
strcat(_pstr,rhs._pstr);
return *this;
}
// s1 += "hello";
String& operator+=(const char * pstr)
{
cout << "String& operator+=(const char * pstr)" << endl;
String tmp(pstr);
*this += tmp;
return *this;
}
// String s1("hello");
// s1[0];
char& operator[](std::size_t index)
{
if(index < size())
{
return _pstr[index];
}
else
{
static char nullchar = '\0';
return nullchar;
}
}
// const String s1("hello");
// s1[0];
const char& operator[](std::size_t index) const
{
if(index < size())
{
return _pstr[index];
}
else
{
static char nullchar = '\0';
return nullchar;
}
}
std::size_t size() const
{
return strlen(_pstr);
}
const char* c_str() const
{
return _pstr;
}
// 友元函数
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const String &);
// 比较大小
friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator<=(const String &, const String &);
friend bool operator>=(const String &, const String &);
// 输入输出运算符重载
friend std::ostream & operator<<(std::ostream &, const String &);
friend std::istream & operator>>(std::istream &, String &);
// 私有成员
private:
char* _pstr;
};
bool operator==(const String & lhs, const String & rhs)
{
return !strcmp(lhs._pstr,rhs._pstr);
}
bool operator!=(const String & lhs, const String & rhs)
{
return strcmp(lhs._pstr,rhs._pstr);
}
bool operator<(const String & lhs, const String & rhs)
{
return strcmp(lhs._pstr,rhs._pstr) < 0;
}
bool operator>(const String & lhs, const String & rhs)
{
return strcmp(lhs._pstr,rhs._pstr) > 0;
}
bool operator<=(const String & lhs, const String & rhs)
{
return strcmp(lhs._pstr,rhs._pstr) <= 0;
}
bool operator>=(const String & lhs, const String & rhs)
{
return strcmp(lhs._pstr,rhs._pstr) >= 0;
}
// cout << string("hello");
std::ostream & operator<<(std::ostream & os, const String & rhs)
{
if(rhs._pstr)
{
os << rhs._pstr;
}
return os;
}
std::istream & operator>>(std::istream & is, String & rhs)
{
if(rhs._pstr)
{
delete [] rhs._pstr;
rhs._pstr = nullptr;
}
vector buffer;
char ch;
while((ch = is.get()) != '\n')
{
buffer.push_back(ch);
}
rhs._pstr = new char[buffer.size() + 1]();
strncpy(rhs._pstr,&buffer[0],buffer.size());
return is;
}
// s1 = s2 + s3;
String operator+(const String & lhs, const String & rhs)
{
cout << "String operator+(const String &, const String &)" << endl;
String tmp(lhs);
tmp += rhs;
return tmp;
}
// s1 + "hello";
String operator+(const String & lhs, const char * pstr)
{
cout << "String operator+(const String &, const char *)" << endl;
String tmp(lhs);
tmp += pstr;
return tmp;
}
// "hello" + s1;
String operator+(const char * pstr, const String & rhs)
{
cout << "String operator+(const char *, const String &)" << endl;
String tmp(pstr);
tmp += rhs;
return tmp;
}
// 测试用例
void test0()
{
String s1; // 默认构造 String()
cout << "s1 = " << s1 << endl;
String s2("hello"); // 拷贝构造 String(const char *pstr)
String s3 = "world"; // 拷贝构造 String(const char *pstr)
String s4(s2); // 拷贝构造 String(const String &rhs)
String s5 = s3; // 拷贝构造 String(const String &rhs)
s1 = s2; // 赋值运算符重载 String &operator=(const String &rhs)
s1 = "hello,"; // 赋值运算符重载 String &operator=(const char *pstr)
s1 += s3; // String &operator+=(const String &rhs)
s1 += "!"; // String &operator+=(const char *pstr)
s1[0] = 'H'; // char &operator[](std::size_t index)
const String s6 = "C++";
cout << s6[0] << endl;
// s6[0] = 'B'; // 错误 const char &operator[](std::size_t index) const
// 比较字符串
String s7 = "hello";
String s8 = "hello";
String s9 = "he";
cout << (s7 == s8) << endl;
cout << (s8 <= s9) << endl;
// 输入输出运算符重载
cin >> s1;
cout << s1 << endl;
//
String s10("B");
String s11("C");
s1 = s10 + s11;
cout << s1 << endl;
s1 = s1 + "D";
cout << s1 << endl;
s1 = "A" + s1;
cout << s1 << endl;
}
int main()
{
test0();
return 0;
}
测试用例运行效果

写时复制原理探究
Q1: 当字符串对象进行复制控制时,可以优化为指向同一个堆空间的字符串,接下来的问题就是何时回收堆空间的字符串内容呢?
引用计数 refcount当字符串对象进行复制操作时,引用计数+1;当字符串对象被销毁时,引用计数-1;只有当引用计数减为0时,才真正回收堆空间上字符串

Q2: 引用计数应该放到哪里?



方案三可行,还可以优化一下
按常规的思路,需要使用两次new表达式(字符串、引用计数);可以优化成只用一次new表达式,因为申请堆空间的行为一定会涉及系统调用,程序员要尽量少使用系统调用,提高程序的执行效率。

引用计数减到1,才真正回收堆空间

CowString代码初步实现
根据写时复制的思想来模拟字符串对象的实现,这是一个非常有难度的任务(源码级),理解了COW的思想后可以尝试实现一下,见01_CoWString.cc
01_CoWString.cc
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
using std::ostream;
//
class CoWString{
public:
CoWString();
~CoWString();
CoWString(const CoWString & rhs);
CoWString(const char * pstr);
CoWString & operator=(const CoWString & rhs);
char & operator[](size_t idx);
const char * c_str() const{ return _pstr; }
size_t size() const{ return strlen(_pstr); }
int use_count(){ return *(int*)(_pstr - kRefCountLength); }
private:
char * malloc(const char * pstr = nullptr){
if(pstr){
return new char[strlen(pstr) + kRefCountLength + 1]() + kRefCountLength;
}else{
return new char[1 + kRefCountLength]() + kRefCountLength;
}
}
void initRefCount(){
*(int*)(_pstr - kRefCountLength) = 1;
}
void release(){
decreaseRefCount();
if(use_count() == 0){
delete [] (_pstr - kRefCountLength);
_pstr = nullptr;
cout << "delete heap" << endl;
}
}
void increaseRefCount(){
++*(int*)(_pstr - kRefCountLength);
}
void decreaseRefCount(){
--*(int*)(_pstr - kRefCountLength);
}
// 私有成员
static const int kRefCountLength = 4;
char * _pstr;
};
// 成员函数实现
CoWString::CoWString()
:_pstr(malloc())
{
cout << "CoWString()" << endl;
initRefCount();
}
CoWString::~CoWString()
{
release();
}
CoWString::CoWString(const CoWString & rhs)
:_pstr(rhs._pstr)
{
increaseRefCount();
cout << "CowString(const CowString&)" << endl;
}
CoWString::CoWString(const char * pstr)
:_pstr(malloc(pstr))
{
strcpy(_pstr,pstr);
initRefCount();
cout << "CoWString(const char *)" << endl;
}
CoWString & CoWString::operator=(const CoWString & rhs)
{
if(this != &rhs){ // 1.还需要考虑自复制
cout << "CowString& operator=(const CowString&)" << endl;
release(); // 2.判断是否回收,原本空间的引用计数-1
_pstr = rhs._pstr; // 3. 浅拷贝
increaseRefCount();
}
return *this;
}
char & CoWString::operator[](size_t idx)
{
if(idx < size())
{
return _pstr[idx];
}
else
{
cout << "out of range" << endl;
static char nullchar = '\0';
return nullchar;
}
}
ostream & operator<<(ostream & os, const CoWString & rhs)
{
os << rhs.c_str();
return os;
}
// 测试用例
void test()
{
CoWString str1;
cout << str1.use_count() << endl;
CoWString str2 = str1;
cout << "str1:" << str1 << endl;
cout << "str2:" << str2 << endl;
cout << str1.use_count() << endl;
cout << str2.use_count() << endl;
cout << "------------------------" << endl;
CoWString str3("hello");
cout << "str3:" << str3 << endl;
cout << str3.use_count() << endl;
CoWString str4 = str3;
cout << "str3:" << str3 << endl;
cout << "str4:" << str4 << endl;
cout << str3.use_count() << endl;
cout << str4.use_count() << endl;
cout << "-------------------------" << endl;
str1 = str3;
cout << "str1:" << str1 << endl;
cout << "str2:" << str2 << endl;
cout << "str3:" << str3 << endl;
cout << "str4:" << str4 << endl;
cout << str1.use_count() << endl;
cout << str2.use_count() << endl;
cout << str3.use_count() << endl;
cout << str4.use_count() << endl;
cout << "-------------------------" << endl;
//读操作不需要重新开辟空间
cout << str1[0] << endl;
//写操作应该重新开辟空间
//也就是写时复制
str1[0] = 'H';
cout << str1[0] << endl;
cout << str3[0] << endl;
cout << str4[0] << endl;
}
int main()
{
test();
return 0;
}
测试用例运行效果

在建立了基本的写时复制字符串类的框架后,发现了一个遗留的问题。
如果str1和str3共享一片空间存放字符串内容。如果进行读操作,那么直接进行就可以了,不用进行复制,也不用改变引用计数;如果进行写操作,那么应该让str1重新申请一片空间去进行修改,不应该改变str3的内容。
cout << str1[0] << endl; //读操作
str1[0] = 'H'; //写操作
cout << str3[0] << endl;//发现str3的内容也被改变了首先会想到运算符重载的方式去解决。但是str1[0]返回值是一个char类型变量。
读操作 cout << char字符 << endl;
写操作 char字符 = char字符;
无论是输出流运算符还是赋值运算符,操作数中没有自定义类型对象,无法重载。而CowString的下标访问运算符的操作数是CowString对象和size_t类型的下标,也没办法判断取出来的内容接下来要进行读操作还是写操作。
—— 思路:创建一个CowString类的内部类,让CowString的operator[]函数返回是这个新类型的对象,然后在这个新类型中对<<和=进行重载,让这两个运算符能够处理新类型对象,从而分开了处理逻辑。
见02_CoWString.cc
02_CoWString.cc
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
using std::ostream;
//
class CoWString{
class CharProxy{
public:
CharProxy(CoWString & self, size_t idx)
:_self(self),
_idx(idx)
{}
char & operator=(char ch);
friend
ostream & operator<<(ostream & os, const CharProxy & rhs);
private:
CoWString & _self;
size_t _idx;
};
public:
CoWString();
~CoWString();
CoWString(const CoWString & rhs);
CoWString(const char * pstr);
CoWString & operator=(const CoWString & rhs);
CharProxy operator[](size_t idx);
const char * c_str() const{ return _pstr; }
size_t size() const{ return strlen(_pstr); }
int use_count(){ return *(int*)(_pstr - kRefCountLength); }
friend
ostream & operator<<(ostream & os, const CharProxy & rhs);
private:
char * malloc(const char * pstr = nullptr){
if(pstr){
return new char[strlen(pstr) + kRefCountLength + 1]() + kRefCountLength;
}else{
return new char[1 + kRefCountLength]() + kRefCountLength;
}
}
void initRefCount(){
*(int*)(_pstr - kRefCountLength) = 1;
}
void release(){
decreaseRefCount();
if(use_count() == 0){
delete [] (_pstr - kRefCountLength);
_pstr = nullptr;
cout << "delete heap" << endl;
}
}
void increaseRefCount(){
++*(int*)(_pstr - kRefCountLength);
}
void decreaseRefCount(){
--*(int*)(_pstr - kRefCountLength);
}
// 私有成员
static const int kRefCountLength = 4;
char * _pstr;
};
// 成员函数实现
CoWString::CoWString()
:_pstr(malloc())
{
cout << "CoWString()" << endl;
initRefCount();
}
CoWString::~CoWString()
{
release();
}
CoWString::CoWString(const CoWString & rhs)
:_pstr(rhs._pstr)
{
increaseRefCount();
cout << "CowString(const CowString&)" << endl;
}
CoWString::CoWString(const char * pstr)
:_pstr(malloc(pstr))
{
strcpy(_pstr,pstr);
initRefCount();
cout << "CoWString(const char *)" << endl;
}
CoWString & CoWString::operator=(const CoWString & rhs)
{
if(this != &rhs){ // 1.还需要考虑自复制
cout << "CowString& operator=(const CowString&)" << endl;
release(); // 2.判断是否回收,原本空间的引用计数-1
_pstr = rhs._pstr; // 3. 浅拷贝
increaseRefCount();
}
return *this;
}
CoWString::CharProxy CoWString::operator[](size_t idx)
{
return CharProxy(*this,idx);
}
ostream & operator<<(ostream & os, const CoWString & rhs)
{
os << rhs.c_str();
return os;
}
char & CoWString::CharProxy::operator=(char ch)
{
if(_idx < _self.size())
{
if(_self.use_count() > 1)
{
// 原本空间的引用计数-1
_self.decreaseRefCount();
// 深拷贝
char * pTemp = _self.malloc(_self._pstr);
strcpy(pTemp,_self._pstr);
// 修改指向
_self._pstr = pTemp;
// 初始化新空间的引用计数
_self.initRefCount();
}
// 执行写操作
_self._pstr[_idx] = ch;
return _self._pstr[_idx];
}
else
{
cout << "out of range" << endl;
static char nullchar = '\0';
return nullchar;
}
}
ostream & operator<<(ostream & os, const CoWString::CharProxy & rhs)
{
if(rhs._idx < rhs._self.size())
{
os << rhs._self._pstr[rhs._idx];
}
else
{
cout << "out of range" << endl;
}
return os;
}
// 测试用例
void test()
{
CoWString str1("hello");
CoWString str2(str1);
CoWString str3(str1);
//读操作不需要重新开辟空间
cout << str1[0] << endl;
/* operator<<(cout,str1.operator[](0)); */
cout << str1.use_count() << endl;
cout << str2.use_count() << endl;
cout << str3.use_count() << endl;
//如果有多个对象共享了空间
//一个对象进行写操作应该重新开辟空间
//也就是写时复制
str1[0] = 'H';
/* (str1.operator[](0)).operator=('H'); */
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str1.use_count() << endl;
cout << str2.use_count() << endl;
cout << str3.use_count() << endl;
str1[0] = 'X';
cout << str1 << endl;
}
int main()
{
test();
return 0;
}
测试用例运行效果

对于读操作,还可以给CharProxy类定义类型转换函数来进行处理。见03_CoWString.cc
03_CoWString.cc
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
using std::ostream;
//
class CoWString{
class CharProxy{
public:
CharProxy(CoWString & self, size_t idx)
:_self(self),
_idx(idx)
{}
char & operator=(char ch);
operator char()
{
cout << "operator char()" << endl;
return _self._pstr[_idx];
}
private:
CoWString & _self;
size_t _idx;
};
public:
CoWString();
~CoWString();
CoWString(const CoWString & rhs);
CoWString(const char * pstr);
CoWString & operator=(const CoWString & rhs);
CharProxy operator[](size_t idx);
const char * c_str() const{ return _pstr; }
size_t size() const{ return strlen(_pstr); }
int use_count(){ return *(int*)(_pstr - kRefCountLength); }
private:
char * malloc(const char * pstr = nullptr){
if(pstr){
return new char[strlen(pstr) + kRefCountLength + 1]() + kRefCountLength;
}else{
return new char[1 + kRefCountLength]() + kRefCountLength;
}
}
void initRefCount(){
*(int*)(_pstr - kRefCountLength) = 1;
}
void release(){
decreaseRefCount();
if(use_count() == 0){
delete [] (_pstr - kRefCountLength);
_pstr = nullptr;
cout << "delete heap" << endl;
}
}
void increaseRefCount(){
++*(int*)(_pstr - kRefCountLength);
}
void decreaseRefCount(){
--*(int*)(_pstr - kRefCountLength);
}
// 私有成员
static const int kRefCountLength = 4;
char * _pstr;
};
// 成员函数实现
CoWString::CoWString()
:_pstr(malloc())
{
cout << "CoWString()" << endl;
initRefCount();
}
CoWString::~CoWString()
{
release();
}
CoWString::CoWString(const CoWString & rhs)
:_pstr(rhs._pstr)
{
increaseRefCount();
cout << "CowString(const CowString&)" << endl;
}
CoWString::CoWString(const char * pstr)
:_pstr(malloc(pstr))
{
strcpy(_pstr,pstr);
initRefCount();
cout << "CoWString(const char *)" << endl;
}
CoWString & CoWString::operator=(const CoWString & rhs)
{
if(this != &rhs){ // 1.还需要考虑自复制
cout << "CowString& operator=(const CowString&)" << endl;
release(); // 2.判断是否回收,原本空间的引用计数-1
_pstr = rhs._pstr; // 3. 浅拷贝
increaseRefCount();
}
return *this;
}
CoWString::CharProxy CoWString::operator[](size_t idx)
{
return CharProxy(*this,idx);
}
ostream & operator<<(ostream & os, const CoWString & rhs)
{
os << rhs.c_str();
return os;
}
char & CoWString::CharProxy::operator=(char ch)
{
if(_idx < _self.size())
{
if(_self.use_count() > 1)
{
// 原本空间的引用计数-1
_self.decreaseRefCount();
// 深拷贝
char * pTemp = _self.malloc(_self._pstr);
strcpy(pTemp,_self._pstr);
// 修改指向
_self._pstr = pTemp;
// 初始化新空间的引用计数
_self.initRefCount();
}
// 执行写操作
_self._pstr[_idx] = ch;
return _self._pstr[_idx];
}
else
{
cout << "out of range" << endl;
static char nullchar = '\0';
return nullchar;
}
}
// 测试用例
void test()
{
CoWString str1("hello");
str1[0] = 'X';
cout << str1 << endl;
// CharProxy提供了类型转换函数 CharProxy->char
// <<处理不了CharProxy对象时会尝试进行转换
cout << str1[0] << endl;
std::string str2(5,str1[0]);
cout << str2 << endl;
}
int main()
{
test();
return 0;
}
测试用例运行效果

短字符串优化(SSO)
当字符串的字符数小于等于15时, buffer直接存放整个字符串;当字符串的字符数大于15时, buffer 存放的就是一个指针,指向堆空间的区域。这样做的好处是,当字符串较小时,直接拷贝字符串,放在 string内部,不用获取堆空间,开销小。

union表示共用体,允许在同一内存空间中存储不同类型的数据。公用体的所有成员共享一块内存,但是每次只能使用一个成员。
class string {
union Buffer{
char * _pointer;
char _local [16];
};
size_t _size;
size_t _capacity;
Buffer _buffer;
};

最佳策略
FaceBook提出的最佳策略,将三者进行结合:
因为以上三种方式,都不能解决所有可能遇到的字符串的情况,各有所长,又各有缺陷。综合考虑所有情况之后,facebook开源的folly库中,实现了一个fbstring, 它根据字符串的不同长度使用不同的拷贝策略, 最终每个fbstring对象占据的空间大小都是24字节。
-
很短的(0~22)字符串用SSO,23字节表示字符串(包括'\0'),1字节表示长度
-
中等长度的(23~255)字符串用eager copy,8字节字符串指针,8字节size,8字节capacity.
-
很长的(大于255)字符串用CoW, 8字节指针(字符串和引用计数),8字节size,8字节capacity.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗