Q 一个 T类
是如何被构造, 释放 拷贝
https://www.cnblogs.com/yocichen/p/10574819.html https://www.kancloud.cn/digest/stl-sources/177267
https://www.cnblogs.com/yocichen/p/10574819.html
https://blog.csdn.net/u013575812/article/details/51171135
啥意思 提前已经分配好内存了(M_start,_M_end_of_storage)。就能解释下吗2个概念。
混淆地方。
可分配空间是vector之外的
class vector : protected _Vector_base<_Tp, _Alloc>
explicit vector(size_type __n)
: _Base(__n, allocator_type())
{
_M_finish = uninitialized_fill_n(_M_start, __n, _Tp());
}
template <class _Tp, class _Alloc>
class _Vector_base {
public:
~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
_Vector_base(const _Alloc&)
: _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
_Vector_base(size_t __n, const _Alloc&)
: _M_start(0), _M_finish(0), _M_end_of_storage(0)
{
_M_start = _M_allocate(__n); //申请n字节大小,返回开始地址
_M_finish = _M_start;//构造时候 没有填充任何元素。vector<int>()
_M_end_of_storage = _M_start + __n; //+n 说明是连续空间
}
protected:
_Tp* _M_start; //表示目前使用空间的 头
_Tp* _M_finish; //表示目前使用空间的 尾
_Tp* _M_end_of_storage; //表示目前使用空间 可用空间。
typedef simple_alloc<_Tp, _Alloc> _M_data_allocator; //内存分配器
//申请n字节空间 malloc
_Tp* _M_allocate(size_t __n)
{ return _M_data_allocator::allocate(__n); }
//释放:具体怎么实现的不清楚
void _M_deallocate(_Tp* __p, size_t __n)
{ _M_data_allocator::deallocate(__p, __n); }
};
_Tp* _M_allocate(size_t __n)
{ return _M_data_allocator::allocate(__n); }
//Return size of allocated storage capacity
//分配的空间大小。在构造时候已经预先分配
size_type capacity() const
{ return size_type(_M_end_of_storage - begin()); }
case1-a:对应的源代码解析中的case1-a情况;
case1-b:对应源码剖析中的case1-b情况:
void push_back(const _Tp& __x) {//在最尾端插入元素
if (_M_finish != _M_end_of_storage) {//若有可用的内存空间
construct(_M_finish, __x);//构造对象
++_M_finish;
}
else//若没有可用的内存空间,调用以下函数,把x插入到指定位置
_M_insert_aux(end(), __x);
}
inline void construct(_T1* __p, const _T2& __value) {
_Construct(__p, __value);
}
template <class _T1, class _T2>
//构造函数执行:new()
//http://www.cplusplus.com/reference/new/operator%20new/
//new (p2) MyClass;
inline void _Construct(_T1* __p, const _T2& __value) {
new ((void*) __p) _T1(__value);
}
//调用构造函数 new (p)MyClass():
template <class _T1>
inline void _Construct(_T1* __p) {
new ((void*) __p) _T1();
}
//调用释放函数
template <class _Tp>
inline void _Destroy(_Tp* __pointer) {
__pointer->~_Tp();
}
construct(__new_finish, __x); //调用构造函数 new(__new_finish)x()
bitcoyp 无论里面是什么元素 6 释放old
construct--> __p->~_Tp()
_M_data_allocator::deallocate(__p, __n);
7 设置三个指针位置到全局变量
void construct(pointer __p, const _Tp &__val) { new (__p) _Tp(__val); }
inline wchar_t*
uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
wchar_t* __result)
{
memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
return __result + (__last - __first);
}
template <class _Tp, class _Alloc>
void
vector<_Tp, _Alloc>::_M_insert_aux(iterator __position)
{
if (_M_finish != _M_end_of_storage) {
construct(_M_finish, *(_M_finish - 1));
++_M_finish;
copy_backward(__position, _M_finish - 2, _M_finish - 1);
*__position = _Tp();
}
else {
const size_type __old_size = size();
const size_type __len = __old_size != 0 ? 2 * __old_size : 1; //step1 len=2n
//new
iterator __new_start = _M_allocate(__len); //step2 alloc len
iterator __new_finish = __new_start;
__STL_TRY {
__new_finish = uninitialized_copy(_M_start, __position, __new_start);
construct(__new_finish);
++__new_finish;
__new_finish = uninitialized_copy(__position, _M_finish, __new_finish);
}
//02 free
__STL_UNWIND((destroy(__new_start,__new_finish),
_M_deallocate(__new_start,__len)));
destroy(begin(), end()); // ~T()
_M_deallocate(_M_start, _M_end_of_storage - _M_start); // 回收地址
//reset
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage = __new_start + __len;
}
}
大家都知道的
别人知道,我不知道的
收益:停留60秒回忆 new 和malloc ,free delete?
收益:停留60秒回忆 strcpy和memcpy区别?
复制的内容不同。
memmove
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void* memmove(void* dest, void* source, size_t count) {
char* ret = (char*)dest;
const char* src =(char*) source;
if (dest <= source || dest >= (source + count)) {
//正向拷贝
//copy from lower addresses to higher addresses
while (count--)
*(ret++) = *(src++);
} else {
//反向拷贝
//copy from higher addresses to lower addresses
while (count--)
*(ret + count) = *(src + count);
}
return ret;
}
uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
wchar_t* __result)
{
memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
return __result + (__last - __first); //直接跳转
}
construct(__new_finish, __x); //调用构造函数 new(__new_finish)x()
void construct(pointer __p, const _Tp &__val) { new (__p) _Tp(__val); }
void destroy(pointer __p) { __p->~_Tp(); }