C++内存管理6-vc6,vs2013评析
先看看vc6allocate的源码:
template<class _Ty> inline
_Ty _FARQ *_Allocate(_PDFT _N, _Ty _FARQ *)
{
if (_N < 0)
_N = 0;
return ((_Ty _FARQ *)operator new((_SIZT)_N * sizeof (_Ty)));
}
template<class _Ty>
class allocator
{
public:
typedef _SIZT size_type;
typedef _PDFT difference_type;
typedef _Ty _FARQ *pointer;
typedef const _Ty _FARQ *const_pointer;
typedef _Ty _FARQ& reference;
typedef const _Ty _FARQ& const_reference;
typedef _Ty value_type;
pointer address(reference _X) const
{
return (&_X);
}
const_pointer address(const_reference _X) const
{
return (&_X);
}
pointer allocate(size_type _N, const void *)
{
return (_Allocate((difference_type)_N, (pointer)0));
}
char _FARQ *_Charalloc(size_type _N)
{ return (_Allocate((difference_type)_N,
(char _FARQ *)0));
}
void deallocate(void _FARQ *_P, size_type)
{
operator delete(_P);
}
void construct(pointer _P, const _Ty& _V)
{
_Construct(_P, _V);
}
void destroy(pointer _P)
{
_Destroy(_P);
}
_SIZT max_size() const
{
_SIZT _N = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _N ? _N : 1)
}
};
其实我们只需要看allocate函数和deallocate,
因为它调用的是
_Ty _FARQ *_Allocate(_PDFT _N, _Ty _FARQ *)
{
if (_N < 0)
_N = 0;
return ((_Ty _FARQ *)operator new((_SIZT)_N * sizeof (_Ty)));
}
其实也就先当与只调用了malloc,对内存并没有做任何优化。
同理我们看看vs2013的alloctor,看看有没有做升级,部分函数如下:
其中_Allocate如下:
可得它也没有做任何改变,有点小失望!!!!
人生总要有不如意嘛,风雨过后总会有彩虹。但是学习不能停止,要不然真的就一无所有啦。