说明:以下涉及的std::string的源代码摘自4.8.2版本。
结论:std::string的拷贝复制是基于引用计数的浅拷贝,因此它们指向相同的数据地址。

// std::string类定义
typedef basic_string<char> string;
template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string
{
private:
// _Alloc_hider是模板类basic_string内嵌struct
struct _Alloc_hider : _Alloc
{
// 唯一构造函数,
// 在构造时使用第一个参数__dat初始化_M_p
_Alloc_hider(_CharT* __dat, const _Alloc& __a)
: _Alloc(__a), _M_p(__dat)
{}

// _M_p为实际存储数据的地方
_CharT* _M_p; // The actual data.
};

private:
_CharT* _M_data() const
{ return _M_dataplus._M_p; }

// 浅拷贝,
// 这正是x2=x1后,两者数据地址相同的原因
_CharT* _M_data(_CharT* __p)
{ return (_M_dataplus._M_p = __p); }

_Rep* _M_rep() const
{
// 这里数组下标是“-1”
return &((reinterpret_cast<_Rep*>(_M_data()))[-1]);
}

// 维护引用计数
struct _Rep_base
{
size_type _M_length;
size_type _M_capacity;
_Atomic_word _M_refcount;
};

// _Rep是模板类basic_string内嵌struct
struct _Rep : _Rep_base
{
// The following storage is init'd to 0 by the linker,
// resulting (carefully) in an empty string with one reference.
// 空的std::string实际都指向了_S_empty_rep_storage,
// 因此它们的数据地址是相同的
static size_type _S_empty_rep_storage[];

static _Rep& _S_empty_rep()
{
void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
return *reinterpret_cast<_Rep*>(__p);
}

_CharT* _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
{
return (!_M_is_leaked() && __alloc1 == __alloc2)
? _M_refcopy() : _M_clone(__alloc1);
}

_CharT* _M_refcopy() throw()
{
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
if (__builtin_expect(this != &_S_empty_rep(), false))
#endif
__gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
return _M_refdata();
} // XXX MT

_CharT* _M_refdata() throw()
{ return reinterpret_cast<_CharT*>(this + 1); }
};

public:
static _Rep& _S_empty_rep()
{
return _Rep::_S_empty_rep();
}

// 不带参数的默认构造函数
// 测试环境_GLIBCXX_FULLY_DYNAMIC_STRING值为0,
// 因此只需要关注_S_empty_rep
basic_string()
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
: _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
{ }
#else
: _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
{ }
#endif

basic_string& assign(const basic_string& __str)
{
// 如果已经相同,则什么也不用做
if (_M_rep() != __str._M_rep())
{
const allocator_type __a = this->get_allocator();
_CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
_M_rep()->_M_dispose(__a);
_M_data(__tmp);
}

return *this;
}

#if __cplusplus >= 201103L
basic_string& assign(basic_string&& __str)
{
this->swap(__str);
return *this;
}
#endif // C++11

basic_string& operator=(const basic_string& __str)
{
return this->assign(__str);
}

private:
// mutable表明const成员函数会修改_M_dataplus
mutable _Alloc_hider _M_dataplus;
};

// 测试代码
// 编译命令:
// g++ -g -o x x.cpp -D_GLIBCXX_DEBUG
#include <stdio.h>
#include <string>

// 如果没有为结构X提供赋值函数,
// 则编译器生成默认的赋值函数
struct X {
std::string str;
};

int main() {
struct X x1, x2;
x1.str = "abc";
// X2指向的_S_empty_rep_storage
printf("%p, %p\n", x1.str.c_str(), x2.str.c_str());

// (gdb) p x1.str._M_dataplus._M_p
// (gdb) p x2.str._M_dataplus._M_p
// 拷贝赋值函数采用的是引用计数,
// 所以x1和x2的数据地址是相同的
x2 = x1;
printf("%p, %p\n", x1.str.c_str(), x2.str.c_str());

// 下面输出的x1和x2数据地址必然不同
x2.str = "123";
printf("%p, %p\n", x1.str.c_str(), x2.str.c_str());
return 0;
}

转载于:https://www.cnblogs.com/aquester/p/10531183.html

std::string的拷贝赋值研究相关推荐

  1. std::string的find问题研究

    https://files-cdn.cnblogs.com/files/aquester/std之string的find问题研究.pdf 目录 目录 1 1. 前言 1 2. find字符串 1 3. ...

  2. vector c++ 赋值_面对拷贝赋值时发生的自我赋值的正确态度时接受而不是防止

    C.62: Make copy assignment safe for self-assignment C.62:保证拷贝赋值对自我赋值安全 Reason(原因) If x = x changes t ...

  3. c++三大函数:拷贝构造(copy ctor)、拷贝赋值(copy op)、析构函数(dtor)

    Class的两个经典分类 Class without pointer member(s) complex Class with pointer member(s) string String clas ...

  4. 侯捷C++->三大函数:拷贝构造、拷贝赋值、析构

    1.拷贝:如果类里带指针不能使用编译器给的那套赋值,仅仅是多了一个指针指向相同的内存(浅拷贝).(指针指向的内容不属于对象本身) 如果类带有指针,big three一定要写 2.拷贝构造:构造函数(函 ...

  5. 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)

    标准C++类std::string的内存共享,值得体会: 详见大牛:https://www.douban.com/group/topic/19621165/ 顾名思义,内存共享,就是两个乃至更多的对象 ...

  6. C++之Big Three:拷贝构造、拷贝赋值、析构函数探究

    涉及到本文所讲知识点的博文: C++之析构函数探究 C++之常引用和浅拷贝探究 C++之一个函数链的简单例子(分文件实现) C++之Big Three:拷贝构造.拷贝赋值.析构函数探究 C++之操作符 ...

  7. 关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题

    使用string时发现了一些坑. 我们知道stl 容器并不是线程安全的,所以在使用它们的过程中往往需要一些同步机制来保证并发场景下的同步更新. 应该踩的坑还是一个不拉的踩了进去,所以还是记录一下吧. ...

  8. std::string用法总结

    c++stdstring 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(const ...

  9. std::string 用法

    string类的构造函数: string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化 string类的字符操作: cons ...

最新文章

  1. 如何短时间提高python面向对象_初识Python面向对象阶段,你们都是怎么过来的?
  2. 深度学习笔记4:Self-Taught Learning and Unsupervised Feature Learning
  3. 关于21年电赛,这些一定要熟悉!
  4. howdoi 简单分析
  5. html:(25):选择器定义和标签选择器
  6. lte核心网由哪些设备组成_投影地面互动的实现由哪些设备组成?「振邦视界」...
  7. js 点击闭包_学习Javascript闭包(Closure)
  8. 使用BFC块级上下文
  9. 安卓案例:View动画 - 弹球碰壁
  10. 1.极限——ε-δ例子_7
  11. [uva11235]Frequent values(RMQ,ST,离散化)
  12. java解析魔兽争霸3录像_《魔兽争霸》的录像,为什么长达半小时的录像大小只有几百 KB?...
  13. 前端工程化--yeoman使用
  14. MAC怎样显示隐藏文件
  15. 电脑键盘上的Alt键的作用
  16. tomcat+nginx配置htpps
  17. 以太网 以太网地址(MAC地址)
  18. BASE理论(基本可用策略+ 最终一致性实现)
  19. 南加州大学计算机科学案例,南加州大学计算机科学录取案例
  20. 高校计算机教师招聘试题及答案,计算机教师招聘试题(含答案、超级、集合版)...

热门文章

  1. iframe嵌套的html高度,iframe 多层嵌套 无限嵌套 高度自适应的解决方案
  2. 用matlab画出M1和M2的ROC曲线,ROC曲线及其matlab实现ROC曲线的绘画
  3. 结束oracle import,Oracle 结束 imp/exp 和 expdp/impdp 进程的正确方法
  4. 为什么企业需要IT资产管理
  5. html5shiv.js和respond.min.js
  6. Node_初步了解(4)小爬虫
  7. 分布式流媒体直播服务器系统 For Linux
  8. XML在线转化为JSON
  9. TCanvas 学习
  10. 《城市建筑美学》读书笔记