从源码角度分析string内存分布


title: 从源码角度分析string内存分布
date: 2020-12-04 20:49:32
tags: C++ | string
categories: source code | C++

文章目录

前言

一直以来都认为:string str = “abc”,str本身是存在栈中的,内部有一个指针指向"abc","abc"存放在堆中。

今天逛某论坛时,看到一种说法:当字符串小于24字节时,存放在栈中,大于时存放在堆栈中。对此说法表示疑惑,接下来就通过实验和一步步的阅读源码来探析string中的内存到底是如何分配存储的。

string是basic_string的特化版本

string是basic_string传入字符类型char的特化版本:

typedef basic_string<char> string;

接下来看下basic_string的接口:

template < class charT,
           class traits = char_traits<charT>,// basic_string::traits_type
           class Alloc = allocator<charT> // basic_string::allocator_type
           > class basic_string;

可以看到basic_string接收一个Alloc参数,即内存分配器参数,默认是allocator,也就是说内存分配任务交给了allocator,那么下一步就是一探allocator的究竟了。

阅读allocator源码

先给出GNU关于allocator的源码链接:allocator.h源码

阅读源码,在allocator.h文件中没有关于内存的任何操作,同时第92行可以看到class allocator继承了 __glibcxx_base_allocator<_Tp>,那么关于内存的操作必然是在class __glibcxx_base_allocator里面了。

从源码角度分析string内存分布

__glibcxx_base_allocator<_Tp>是什么

在c++allocator.h文件第35行可以找到#define __glibcxx_base_allocator __gnu_cxx::new_allocator,也就是说

我们要找的__glibcxx_base_allocator就是__glibcxx_new_allocator。附上c++allocator.h的源码链接:

c++allocator.h源码
从源码角度分析string内存分布

new_allocator.h——真正进行内存分配工作的地方

new_allocator.h源码

00001 // Allocator that wraps operator new -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009, 2010
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file ext/new_allocator.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _NEW_ALLOCATOR_H
00031 #define _NEW_ALLOCATOR_H 1
00032 
00033 #include <bits/c++config.h>
00034 #include <new>
00035 #include <bits/functexcept.h>
00036 #include <bits/move.h>
00037 
00038 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   using std::size_t;
00043   using std::ptrdiff_t;
00044 
00045   /**
00046    *  @brief  An allocator that uses global new, as per [20.4].
00047    *  @ingroup allocators
00048    *
00049    *  This is precisely the allocator defined in the C++ Standard. 
00050    *    - all allocation calls operator new
00051    *    - all deallocation calls operator delete
00052    */
00053   template<typename _Tp>
00054     class new_allocator
00055     {
00056     public:
00057       typedef size_t     size_type;
00058       typedef ptrdiff_t  difference_type;
00059       typedef _Tp*       pointer;
00060       typedef const _Tp* const_pointer;
00061       typedef _Tp&       reference;
00062       typedef const _Tp& const_reference;
00063       typedef _Tp        value_type;
00064 
00065       template<typename _Tp1>
00066         struct rebind
00067         { typedef new_allocator<_Tp1> other; };
00068 
00069       new_allocator() throw() { }
00070 
00071       new_allocator(const new_allocator&) throw() { }
00072 
00073       template<typename _Tp1>
00074         new_allocator(const new_allocator<_Tp1>&) throw() { }
00075 
00076       ~new_allocator() throw() { }
00077 
00078       pointer
00079       address(reference __x) const { return std::__addressof(__x); }
00080 
00081       const_pointer
00082       address(const_reference __x) const { return std::__addressof(__x); }
00083 
00084       // NB: __n is permitted to be 0.  The C++ standard says nothing
00085       // about what the return value is when __n == 0.
00086       pointer
00087       allocate(size_type __n, const void* = 0)
00088       { 
00089     if (__n > this->max_size())
00090       std::__throw_bad_alloc();
00091 
00092     return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
00093       }
00094 
00095       // __p is not permitted to be a null pointer.
00096       void
00097       deallocate(pointer __p, size_type)
00098       { ::operator delete(__p); }
00099 
00100       size_type
00101       max_size() const throw() 
00102       { return size_t(-1) / sizeof(_Tp); }
00103 
00104       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00105       // 402. wrong new expression in [some_] allocator::construct
00106       void 
00107       construct(pointer __p, const _Tp& __val) 
00108       { ::new((void *)__p) _Tp(__val); }
00109 
00110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00111       template<typename... _Args>
00112         void
00113         construct(pointer __p, _Args&&... __args)
00114     { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
00115 #endif
00116 
00117       void 
00118       destroy(pointer __p) { __p->~_Tp(); }
00119     };
00120 
00121   template<typename _Tp>
00122     inline bool
00123     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00124     { return true; }
00125   
00126   template<typename _Tp>
00127     inline bool
00128     operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00129     { return false; }
00130 
00131 _GLIBCXX_END_NAMESPACE_VERSION
00132 } // namespace
00133 
00134 #endif

阅读以上源码,可以发现所有关于内存的操作都是通过global new来实现的,在关于new_allocator文件的官方参阅手册上也有这样一段话:

Detailed Description

template class __gnu_cxx::new_allocator< _Tp >

An allocator that uses global new, as per [20.4].

This is precisely the allocator defined in the C++ Standard.

  • all allocation calls operator new
  • all deallocation calls operator delete.

Definition at line 54 of file new_allocator.h.

结论

至此,通过阅读GNU相关allocator的源码,我们可以小结一下:

  1. string是basic_string的特化版本。

  2. basic_string接收一个Alloc参数进行内存分配,默认是allocator。

  3. allocator继承了basic_allocator<_Tp>,basic_allocator<_Tp>就是new_allocator<_Tp>

  4. new_allocator<_Tp>是一个使用global new的内存分配器,内部所有关于内存分配和内存释放的操作都是通过operator new 和operator delete来进行的,而operator new是向堆请求内存的。

因此:对于string str = “abc”,"abc"是存放在堆中的。

写在最后

本文通过一步步的阅读源码,从string逐渐解析到最终为之分配内存的内存分配器new_allocator,确定string中存储的字符串是位于堆中的。在上述分析中,如有错误还请不吝赐教。

上一篇:针对一个class写出它的内存管理池以及总结出allocator类(三个版本)


下一篇:Redis info memory信息说明