std :: string为1个字符串分配2次内存 [英] std::string allocate memory 2 times for 1 string
问题描述
#include <iostream>
#include <string>
void* operator new(size_t size) {
std::cout << "Allocated: " << size << " Bytes\n";
return malloc(size);
}
void operator delete(void* var) {
std::cout << "Deleted\n";
free(var);
}
int main() {
std::string name0 = "Ahmed Zaki Marei";
//std::string name1 = "Lara Mohammed";
std::cout << name0 << "\n";
//std::cout << name1 << "\n";
}
当我尝试运行此代码时,它给出以下输出:
When I try to run this code it gives me this output:
Allocated: 8 Bytes
Allocated: 32 Bytes
Ahmed Zaki Marei
Deleted
Deleted
为什么先分配8个字节然后分配32个字节?有人可以解释吗?谢谢!:)
why does it allocate 8 bytes first then allocate 32 bytes? could anyone explain it, please? thx! :)
推荐答案
以C.M.已经在注释中指出:这是Debug模式下的MS STL行为,与_ITERATOR_DEBUG_LEVEL有关.发行版中一切正常.
As C.M. already pointed out in the comments: this is MS STL behavior in Debug mode, smth to do with _ITERATOR_DEBUG_LEVEL. Everything is fine in Release.
很好奇,所以我自己进行了测试,并且堆栈在 new
的第一个断点处读取了此代码:
Was curious so I tested it myself and the stack reads this on first breakpoint in new
:
operator new(unsigned int size)
std::_Default_allocate_traits::_Allocate(const unsigned int _Bytes)
std::_Allocate<8,std::_Default_allocate_traits,0>(const unsigned int _Bytes)
std::allocator<std::_Container_proxy>::allocate(const unsigned int _Count)
std::_Container_proxy_ptr12<std::allocator<std::_Container_proxy>>::_Container_proxy_ptr12<std::allocator<std::_Container_proxy>>(std::allocator<std::_Container_proxy> & _Al_, std::_Container_base12 & _Mycont)
std::string::basic_string<char,std::char_traits<char>,std::allocator<char>>(const char * const _Ptr)
main()
让我们看一下 basic_string
构造函数:
Let us look at the basic_string
constructor:
basic_string(_In_z_ const _Elem* const _Ptr) : _Mypair(_Zero_then_variadic_args_t{}) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
以及 _Container_proxy_ptr
的真正含义是:
#if _ITERATOR_DEBUG_LEVEL == 0
#define _GET_PROXY_ALLOCATOR(_Alty, _Al) _Fake_allocator()
template <class _Alloc>
using _Container_proxy_ptr = _Fake_proxy_ptr_impl;
#else // _ITERATOR_DEBUG_LEVEL == 0
#define _GET_PROXY_ALLOCATOR(_Alty, _Al) static_cast<_Rebind_alloc_t<_Alty, _Container_proxy>>(_Al)
template <class _Alloc>
using _Container_proxy_ptr = _Container_proxy_ptr12<_Rebind_alloc_t<_Alloc, _Container_proxy>>;
#endif // _ITERATOR_DEBUG_LEVEL == 0
它是Debug中的 _Container_proxy_ptr12
(最终调用 .allocate(1)
)和 _Fake_proxy_ptr_impl
(不执行任何操作).
It is _Container_proxy_ptr12
(eventually calling .allocate(1)
) in Debug
and _Fake_proxy_ptr_impl
(doing nothing) in Release.
这篇关于std :: string为1个字符串分配2次内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!